After solving that problem i have a new one:
Situation: I have C Project which uses a library written in C++. From C there are only calls to functions, however the implementation of these functions use classes.
Details:
The library compiles without errors, but compiling the C project returns following errors:
...
Start.cpp:(.text+0x860): undefined reference to `InitialCondition::Load(std::string, bool)'
Start.cpp:(.text+0x227b): undefined reference to `InitialCondition::SetMachIC(double)'
...
general:
<calling library implementation>:(...): undefined reference to <a class>::<a method>
This normally indicates that the library is missing, but in this case i can use most of functions of that library – it fails only if there are some classes used in the implementation.
Here's an example:
In the C++ Library:
Start.h
...
#ifdef __cplusplus
extern "C"
{
#endif
void start();
...
#ifdef __cplusplus
}
#endif
Start.cpp
#include "Start.h"
#include "InitialCondition"
InitialCondition* initCond;
void start()
{
// initCond is set somewhere here
...
initCond->SetMachIC(1.0); // (A) has no reference
initCond->SetAlphaDegIC(2.0); // (B) has a reference
}
InitCondition.h
// includes etc.
class InitialCondition : public ABaseClass
{
public:
void SetMachIC(double mach);
void SetAlphaDegIC(double a)
{
// inline implementation
}
};
Calling from C:
#include "Start.h"
void example()
{
start();
}
Since the library is build using cmake
, here's the part associated CMakeLists.txt
:
set(INITIALISATION_SRC InitialCondition.cpp)
set(INITIALISATION_HDR InitialCondition.h)
add_library(Init ${INITIALISATION_HDR} ${INITIALISATION_SRC})
set_target_properties (Init PROPERTIES
VERSION "${LIBRARY_VERSION}")
if(BUILD_SHARED_LIBS)
set_target_properties (Init PROPERTIES
SOVERSION ${LIBRARY_SOVERSION}
FRAMEWORK ON)
endif()
install(TARGETS Init LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION lib
# For Mac
FRAMEWORK DESTINATION "/Library/Frameworks")
install(FILES ${INITIALISATION_HDR} DESTINATION include/Project/initialization)
( There's a long list of of .cpp
and .h
files i've shorted this a bit )
Note: Btw. there are still some not-line method calls without such an error.
Note: This files are in a sub directory. its added using add_subdirectory(initialization)
Note: nm
list of the compiled library contains: InitialCondition9SetMachICEd
Why there's no reference of these methods when called from C – even if library is linked and an implementation of the methods is available?
edit:
Here are the commands used for C:
compile:
gcc -I"<include dir>" -O0 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Test.d" -MT"src/Test.d" -o "src/Test.o" "../src/Test.c"
link:
gcc -L"<searchpath>" -shared -o "libTest.dll" ./src/Test.o -l<library name> -lstdc++
Test.d
contains a list of all files included with #include