Fallenreaper
Fallenreaper

Reputation: 10704

objective-c Linker error with a c++ class

I have a C++ class, which is referenced, and then i am going to declare a variable. I declare in objective-c:

cObject obj = cObject();
obj.myFunct("test");

but there seems to be a linker error. It says:

Undefined symbols for architecture armv7:
  "cObject::cObject()", referenced from:
    -[...] in xxx.o
    ___cxxx_global_var_init in xxx.o
"cObject::myFunct(std::__1:basic_string<char,str::__1::char_traits<char>, str::__1::allocator<char> >)", referenced from:
    -[///] in xxx.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1(use -v to see invocation)

Edit: First error resolved in comments: Dont define an empty constructor resolved the first part. The second part refers to calling obj.myFunct("test");

Upvotes: 1

Views: 709

Answers (2)

Fallenreaper
Fallenreaper

Reputation: 10704

This is a 2 part answer.

The first part of the error is because i have an empty constructor. It is not allowed to be empty and will throw an error if it is. To resolve this, i had removed the constructor and the compiler creates one by default.

The other part of the question is from compile issues. What do I mean? I mean, the file in this case, test.h has associated data that was not being compiled. I was helped through the comments to resolve this, but ultimately went into the XCode project, and added the .cpp to the "Compile Sources" section by clicking the little plus button associated with the section. After that source was successfully compiled, the program ran, and worked perfectly, calling the functions, and declaring classes correctly, as planned.

I am not taking credit for what @AdamRosenfield, @EdS., @H2CO3 had helped me through it

Upvotes: 0

Adam Rosenfield
Adam Rosenfield

Reputation: 400572

You need to define your class's default constructor and the myFunct function somewhere:

cObject::cObject()
{
    ...
}

return_type cObject::myFunct(std::string arg1)
{
    ...
}

If they are defined, make sure that you're linking in the object file they're defined in (i.e. add the source file to your Makefile/project file/command line/etc.).

Upvotes: 1

Related Questions