Reputation: 3
I am using xcode to create a static library with C, I seem to get an error Undefined symbols for architecture i386
.
The static library project, include three files: fun.c
, testFun.cpp
, testFun.h
Here is testFun.cpp
#include "testFun.h"
extern void test_c_fun();
void TestFun::test()
{
printf("# TestFun c++ # ");
test_c_fun();
}
and here is the fun.c
#include <stdio.h>
void test_c_fun()
{
printf("# test_c_fun #");
}
When I build with "IOS Device" and "iPhone Retina(4-inch)", I get two x.a file.
Using lipo
tool with -create
param to output the new x.a, supporting arm
and i386
.
add the x.a to my project, and include the testFun head file now code:
TestFun tf;
tf.test();
Then building it, I get these errors
Undefined symbols for architecture i386: "test_c_fun()", referenced from: TestFun::test() in libstatistic.a(testFun.o) ld: symbol(s) not found for architecture i386
When I hide the c-fun call (test_c_fun), then the build is successful!
look like:
#include "testFun.h"
extern void test_c_fun();
void TestFun::test()
{
printf("# TestFun c++ # ");
//test_c_fun();
}
Why can it not work with C file?
Upvotes: 0
Views: 711
Reputation: 9395
In testFun.cpp, declare C functions with extern C
as
extern "C" void test_c_fun();
C style function have different name mangling rules. When you declare a C function in .cpp file, it will treat that function as a C++ function.
When you will add extern "C"
before declaration, it will see that function as a C function.
Upvotes: 1