Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

c++ - Undefined symbols for architecture i386:

I've recently moved over to a mac, and am struggling using the command line compilers. I'm using g++ to compile, and this builds a single source file fine. if I try to add a custom header file, when I try to compile using g++ I get undefined symbols for architecture i386. The programs compile fine in xCode however. Am I missing something obvious?

tried using g++ -m32 main.cpp... didn't know what else to try.


Okay, The old code compiled... Have narrowed it down to my constructors.

class Matrix{
public:
    int a;
    int deter;

    Matrix();
    int det();
};

#include "matrix.h"


Matrix::Matrix(){
    a = 0;
    deter = 0;
}

int Matrix::det(){
    return 0;

}

my error is Undefined symbols for architecture x86_64: "Matrix::Matrix()", referenced from: _main in ccBWK2wB.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status

my main code has

#include "matrix.h"
int main(){
    Matrix m;

    return 0;
} 

along with the usual

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you’ve got three files:

  • matrix.h, a header file that declares the Matrix class;
  • matrix.cpp, a source file that implements Matrix methods;
  • main.cpp, a source file that defines main() and uses the Matrix class.

In order to produce an executable with all symbols, you need to compile both .cpp files and link them together.

An easy way to do this is to specify them both in your g++ or clang++ invocation. For instance:

clang++ matrix.cpp main.cpp -o programName

or, if you prefer to use g++ — which Apple haven’t updated in a while, and it looks like they won’t in the foreseeable future:

g++ matrix.cpp main.cpp -o programName

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...