Undefined symbols for architecture i386:

后端 未结 3 752
囚心锁ツ
囚心锁ツ 2020-12-20 22:37

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

相关标签:
3条回答
  • 2020-12-20 23:24

    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
    
    0 讨论(0)
  • 2020-12-20 23:35

    Did you actually define the Box constructor somewhere? (like Line.cpp)

    0 讨论(0)
  • 2020-12-20 23:38

    is not the case here, but it may happen to be the you forget to put the class name with ::

    for example:


    a good format:

    foo.h

    class Foo{
    public:
        Foo();
        void say();
    private:
        int x;
    };
    

    foo.cpp

    Foo::Foo(){
        this->x = 1;
    }
    
    void Foo::say(){
        printf("I said!\n");
    }
    

    a bad format

    foo.h

    class Foo{
    public:
        Foo();
        void say();
    private:
        int x;
    }
    

    foo.cpp

    Foo::Foo(){
        this->x = 1;
    }
    
    //I always mistake here because I forget to put the class name with :: and the xcode don't show this error.
    void say(){
        printf("I said!\n");
    }
    
    0 讨论(0)
提交回复
热议问题