0

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

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

2 Answers2

1

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.).

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • i have in an imported header file – Fallenreaper Nov 06 '12 at 21:01
  • @Fallenreaper: The header file isn't enough. You need to actually write the code for the functions in a separate source file or inline the definitions into the header file. – Adam Rosenfield Nov 06 '12 at 21:02
  • 1
    i have import "test.h" and then an associated cpp file called "test.cpp" which contains the actual definitions Is it not connecting the reference between the header and the cpp file for some reason? I thought it was automatic. Maybe there is something i need to add to the header to know the definitions are located in "test.cpp" ?? – Fallenreaper Nov 06 '12 at 21:03
  • 1
    Don't declare or define a default constructor if it doesn't do anything. – Ed S. Nov 06 '12 at 21:04
  • 1
    @Fallenreaper Are you sure the `.cpp` file is getting compiled? –  Nov 06 '12 at 21:04
  • @EdS. it doesnt do anything. So should i just remove that from teh defined code? – Fallenreaper Nov 06 '12 at 21:05
  • 1
    Yes, the compiler will generate one for you. – Ed S. Nov 06 '12 at 21:06
  • @EdS. the one compiler error has been removed because of the empty constructor. Still has a error accessing myFunct – Fallenreaper Nov 06 '12 at 21:07
  • @H2CO3 it is getting compiled. – Fallenreaper Nov 06 '12 at 21:07
  • 1
    @Fallenreaper: Are you 100% positive that `test.cpp` is getting compiled? Does it give you an error if you introduce a deliberate syntax error? Do you have an object file named `test.o` anywhere in your intermediate build directory? If so, what is the output of running `nm test.o | grep myFunct` from the terminal? – Adam Rosenfield Nov 06 '12 at 21:12
  • standby while i look through build directory and test – Fallenreaper Nov 06 '12 at 21:13
  • @AdamRosenfield How do you find out where the intermediate build directory is? I cant seem to find it for XCode – Fallenreaper Nov 06 '12 at 21:17
  • 1
    @Fallenreaper: See [this question](http://stackoverflow.com/questions/5952782/xcode-4-where-is-the-build-folder). – Adam Rosenfield Nov 06 '12 at 21:18
  • I did it 1 step easier. I went to the project and clicked on "Compiled Sources". It is no in there, so i guess it isnt getting compiled. Should i add it to it? If this were not through XCode, it is along the lines of saying that my file was not added to the makefile. How should i go about adding it via XCode? Answer: THere is a small plus button you can click. – Fallenreaper Nov 06 '12 at 21:24
  • Issue Resolved, compiles, and runs perfectly. Im not sure who wants to post the answer, If no one 1, i will. :) THANKS EVERYONE! :) – Fallenreaper Nov 06 '12 at 21:29
0

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

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129