Reputation: 351
I am trying to make a shared library in scons but it keeps telling me there is an error because there is an undefined reference to some functions I wrote. I include the .c file from which I am trying to make the shared library. For some reason it is recognizing the cpp file I am inputting but not the .c file.
Your help would be much appreciated`
import os
Import('env')
env = env.Clone()
env.Append(CPPPATH=['include'])
env.Append(LIBS=['serial'])
env.Append(LIBPATH=['/usr/local/lib'])
env.Append(LIBS=['boost_date_time','boost_system', 'boost_thread'])
lib = []
binaries = []
lib.extend(env.SharedLibrary('File1.c', 'File2.cpp']))
print "the error is here"
test_env = env.Clone()
test_env.Append(LIBS=['Program'], LIBPATH=['.'])
binaries.extend([
test_env.Program('test_Program', Glob('test/test_Adafruit.cpp')),
])
Return('lib', 'binaries')
during the linking phase: I get the errors like:
LINK build/test_Program
build/test_Program/libProgram.so:
undefined reference to function(int, sensor_xyz_s*)'
Upvotes: 0
Views: 544
Reputation: 15972
C and C++ don't have the same calling conventions, so C++ does not recognize C functions from other objects. If you're trying to call a C function in File1.c from a C++ function in File2.cpp, you either need to (1) make sure to declare the C function with extern "C"
, (2) explicitly use the C++ compiler to create File1.o, or (3) rename File1 to File1.cpp (assuming that your C code is valid C++). The cleanest solution is probably option (1).
To set the C compiler in your SConscript, use:
env['CC'] = env['CXX']
To tell the C++ code that function
is a C function, change the declaration of the C function to the following. Note that this is just the declaration of the function -- if the declaration is in a header used by both C and C++, you need to wrap the extern "C" so that it's hidden from the C compiler (which may not understand it).
#ifdef __cplusplus
extern "C" {
#endif
void function(int, sensor_xyz_s*);
#ifdef __cplusplus
}
#endif
Upvotes: 2