Creating and using static lib in xcode

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

So, as a test, I created a BSD static C library project and simply added the following code:

//test.h

int testFunction();

//test.cpp

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

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

Now I want to use it in another program to create a new xcode project (cocoa application) Enter 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-click on the project -> add -> existing framework -> add another. I selected the .a file and added it to 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

macosx ++. , , - , dll Windows. .

+3
2

, Test.cpp, Test.c? .c ​​ .

Test.c, extern "C" ++. :.

#ifdef __cplusplus
extern "C" {
#endif

int testFunction();

#ifdef __cplusplus
}
#endif

. ++ FAQ lite entry .

+1

(.a ) - - , .

, Testlib.h - just #include Test.h main.cpp.

+1

Source: https://habr.com/ru/post/1738867/


All Articles