Undefined symbols for architecture i386:

18,464

Solution 1

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");
}

Solution 2

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

Share:
18,464
maccard
Author by

maccard

I'm a masters student in Computer Science.

Updated on June 13, 2022

Comments

  • maccard
    maccard almost 2 years

    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

  • RedX
    RedX about 13 years
    And you are missing a ; after the constructor declaration.
  • maccard
    maccard about 13 years
    Constructor is defined. The code compiles in XCode. and no, have a ; at the end of that line, that's just a type sorry.
  • Ken Aspeslagh
    Ken Aspeslagh about 13 years
    and you're including Matrix.cpp in the command line too, right?
  • maccard
    maccard about 13 years
    No, I wasn't including matrix.cpp in the command line. Thanks!
  • 2am
    2am over 10 years
    wow, this one did it for me.. I'd say not enough c++ experience from my side. I thought the issue was with Xcode. But no, it was my mistake. Will be careful now.
  • Can Ürek
    Can Ürek over 9 years
    Perfect explain. Thanks.