I have a static library built from another project and I want to use it in new project. My CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(smah)
add_library(lotrlib STATIC IMPORTED)
set_target_properties(lotrlib PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/libdmggameserver.a)
set_target_properties(lotrlib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES /home/plomba/dmg/dmglib/gtgameserver)
add_executable(smah main.cpp)
target_link_libraries(smah lotrlib)
install(TARGETS smah RUNTIME DESTINATION bin)
and my main.cpp:
#include <iostream>
#include "onetorulethemall.h"
int main(int argc, char **argv) {
const double inputValue = std::stod(argv[1]);
std::cout << inputValue << std::endl;
serveDarkLordMelkor();
return 0;
}
The directory '/home/plomba/dmg/dmglib/gtgameserver' does contain onetorulethemall.h file and this file does declare function 'serveDarkLordMelkor()'
But when I try to build this I get
/home/plomba/projects/smah/main.cpp:8: undefined reference to `serveDarkLordMelkor()
also I see #include "onetorulethemall.h" in red in my IDE saying that file was not found and serveDarkLordMelkor is not suggested in autocompletion when I start typing it
The problem was in mixing C and C++ code. The included libdmggameserver.a is written in C and so its header had to be wrapped in
#ifdef __cplusplus
extern "C" {
#endif
...header file content...
#ifdef __cplusplus
}
#endif