views:

399

answers:

2

I am trying to create a static library in xcode and link to that static library from another program.

So as a test i have created a BSD static C library project and just added the following code:

//Test.h

int testFunction();

//Test.cpp

#include "Test.h"
int testFunction() {
return 12;
}

This compiles fine and create a .a file (libTest.a).

Now i want to use it in another program so I create a new xcode project (cocoa application) Have the following code:

//main.cpp

#include <iostream>
#include "Testlib.h"

int main (int argc, char * const argv[]) {
    // insert code here...
    std::cout << "Result:\n" <<testFunction();
    return 0;
}

//Testlib.h

extern int testFunction();

I right clicked on the project -> add -> existing framework -> add other Selected the .a file and it added it into the project view.

I always get this linker error:

Build TestUselibrary of project TestUselibrary with configuration Debug

Ld build/Debug/TestUselibrary normal x86_64
cd /Users/myname/location/TestUselibrary
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk 
-L/Users/myname/location/TestUselibrary/build/Debug  
 -L/Users/myname/location/TestUselibrary/../Test/build/Debug 
-F/Users/myname/location/TestUselibrary/build/Debug 
-filelist /Users/myname/location/TestUselibrary/build/TestUselibrary.build/Debug/TestUselibrary.build/Objects-normal/x86_64/TestUselibrary.LinkFileList 
-mmacosx-version-min=10.6 -lTest -o /Users/myname/location/TestUselibrary/build/Debug/TestUselibrary



Undefined symbols:
  "testFunction()", referenced from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I am new to macosx development and fairly new to c++. I am probably missing something fairly obvious, all my experience comes from creating dlls on the windows platform. I really appreciate any help.

A: 

You don't add the library (.a file) as a framework - it's just a library - add it to the project like you would add a source file.

Also note that you don't need Testlib.h - just #include the original Test.h in main.cpp.

Paul R
Thanks for your answer butIt has the same result whether I add it using add framework or adding it in as if I would a source file. Still get the same linking error.I originally linked straight to the .h file but thought that might be what was causing this error so I changed it to this way, but same result. But yeah I will be changing back to linking straight to it as I don't want to have to change 2 header files. This way should still work the same right?
Alasdair Morrison
Small point - you don't *link* to header files (.h files) - you just #include them. Question: do you want your static library to have a C API or a C++ API ?
Paul R
Ah sorry, I didn't mean link, I just meant add them to the xcode project. In the end i want it to have a C API which calls ObjC functions, so I can link it in a c++ project. So the c++ code will call the C functions in the static lib which will then call objC code contained in the lib. This was just a test to see if I could get normal static libs to work without objC.
Alasdair Morrison
OK - I think gf has now given you the best solution for a C library API with a C/C++/ObjC-compatible header. Just use .c files for your library source and then add the `extern "C" {` etc boilerplate from gf's answer to your .h file(s).
Paul R
A: 

Are you sure that the libraries source file is named Test.cpp and not Test.c? With .c i get exactly the same error.

If it is Test.c you need to add extern "C" to the header for C++. E.g.:

#ifdef __cplusplus
extern "C" {
#endif

int testFunction();

#ifdef __cplusplus
}
#endif

See e.g. the C++ FAQ lite entry for more details.

Georg Fritzsche
He shouldn't need to do that as both the library and the test harness are .cpp.
Paul R
Right, i overread that its `Test.cpp` - clarified.
Georg Fritzsche
Yeah your right I called it Test.c instead of Test.cpp, sorry didn't realise I had made such a stupid mistake.
Alasdair Morrison