CMake "undefined function reference"

I am trying to use CMake to bind a library ( driver BNO055 ). Since the BNO055 driver does not use CMake, and it has not been changed for about a year, I decided to just download the source files and put them in my project.

Then I use CMake to create the library and link it.

The problem is that the link does not seem to work. When I compile the project, I get an undefined reference to <function> error, where <function> is the function defined by the BNO055 driver.

Am I creating or linking a library incorrectly?
Do I need to do something to define these functions?

In order not to insert in 200 lines of code, here is a simplified main.cpp , which creates the same error as the real main.cpp . If you want to see the real main.cpp , follow the link below to the Github repository

 #include "bno055.h" #include "mraa.hpp" struct bno055_t bno055; mraa::I2c *i2c(0); int main() { bno055_init(&bno055); i2c->address(0x29); } 

CMakeLists.txt

 cmake_minimum_required(VERSION 2.8.4) project(imc-server) # CMake # -- Config set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread") # Global # -- Include include(ExternalProject) # BNO055 # -- Include include_directories(${CMAKE_SOURCE_DIR}/bno055) set(SOURCE_FILES ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/bno055/bno055.h ${CMAKE_SOURCE_DIR}/bno055/bno055.c) # MRAA # -- Build externalProject_add(mraa GIT_REPOSITORY https://github.com/intel-iot-devkit/mraa.git GIT_TAG v0.7.5 ) # Compile # -- Source set(SOURCE_FILES ${SOURCE_FILES} main.cpp) # -- Create add_executable(imc-server ${SOURCE_FILES}) add_dependencies(imc-server mraa) 

The corresponding part of the assembly log

 [ 90%] Linking CXX executable imc-server CMakeFiles/imc-server.dir/test.cpp.o: In function `main': /home/noah/Documents/Inertial-Motion-Capture/imc-server/test.cpp:8: undefined reference to `bno055_init(bno055_t*)' CMakeFiles/imc-server.dir/test.cpp.o: In function `mraa::I2c::address(unsigned char)': /usr/local/include/mraa/i2c.hpp:99: undefined reference to `mraa_i2c_address' collect2: error: ld returned 1 exit status make[2]: *** [imc-server] Error 1 make[1]: *** [CMakeFiles/imc-server.dir/all] Error 2 make: *** [all] Error 2 

Github Project ( 39a6196 )
Build log

+5
source share
2 answers

The problem was that the BNO055 library was written in C, and my program was written in C ++.

I learned that to use the function defined in a C program in a C ++ program, you need to wrap the inclusion of the C library in the extern "C" {} block as follows:

 extern "C" { #include "bno055.h" } #include "mraa.hpp" struct bno055_t bno055; mraa::I2c *i2c(0); int main() { bno055_init(&bno055); i2c->address(0x29); } 
+4
source

Remove the title from SOURCE_FILES.

 set(SOURCE_FILES ${SOURCE_FILES} # ${CMAKE_SOURCE_DIR}/bno055/bno055.h ${CMAKE_SOURCE_DIR}/bno055/bno055.c) 

CMake should find the necessary header on its own. Additional features can be found at include_directories

0
source

All Articles