简体   繁体   中英

clang: error: linker command failed with exit code 1 (use -v to see invocation) when linking library in cmake

I'm trying to link this library to my project, I'm using CLion as my IDE and CMake to link. I have tried to follow all kinds of tutorials but no result. Is there another way of doing this? I'm a beginner in C++ and I come from a more higher level language. Thank you in advance

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)

project(psapitest)

add_library(
    psmoveapi-lib
    ${PROJECT_SOURCE_DIR}/main.cpp
    ${PROJECT_SOURCE_DIR}/psmoveapi/build/psmove_config.h
    ${PROJECT_SOURCE_DIR}/psmoveapi/include/psmove.h
)

add_executable(test-app main.cpp)

target_link_libraries(test-app PRIVATE psmoveapi-lib)

Project structure: 在此处输入图片说明

Console error:

====================[ Build | test-app | Debug ]================================
 /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build.    /Users/admin/CLionProjects/psapitest/build --target test-app -- -j 2
Scanning dependencies of target psmoveapi-lib
[ 25%] Building CXX object CMakeFiles/psmoveapi-lib.dir/main.cpp.o
[ 50%] Linking CXX static library libpsmoveapi-lib.a
[ 50%] Built target psmoveapi-lib
Scanning dependencies of target test-app
[ 75%] Building CXX object CMakeFiles/test-app.dir/main.cpp.o
[100%] Linking CXX executable test-app
Undefined symbols for architecture x86_64:
  "_psmove_connect", referenced from:
  _main in main.cpp.o
  "_psmove_connection_type", referenced from:
  _main in main.cpp.o
  "_psmove_count_connected", referenced from:
  _main in main.cpp.o
  "_psmove_disconnect", referenced from:
  _main in main.cpp.o
  "_psmove_get_accelerometer", referenced from:
  _main in main.cpp.o
   "_psmove_get_battery", referenced from:
  _main in main.cpp.o
  "_psmove_get_buttons", referenced from:
  _main in main.cpp.o
  "_psmove_get_gyroscope", referenced from:
  _main in main.cpp.o
  "_psmove_get_magnetometer", referenced from:
  _main in main.cpp.o
  "_psmove_get_serial", referenced from:
  _main in main.cpp.o
 "_psmove_get_temperature", referenced from:
  _main in main.cpp.o
  "_psmove_get_temperature_in_celsius", referenced from:
  _main in main.cpp.o
  "_psmove_get_trigger", referenced from:
  _main in main.cpp.o
  "_psmove_init", referenced from:
  _main in main.cpp.o
  "_psmove_poll", referenced from:
  _main in main.cpp.o
  "_psmove_set_leds", referenced from:
  _main in main.cpp.o
  "_psmove_set_rumble", referenced from:
  _main in main.cpp.o
  "_psmove_update_leds", referenced from:
  _main in main.cpp.o
  "_psmove_util_sleep_ms", referenced from:
  _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [test-app] Error 1
make[2]: *** [CMakeFiles/test-app.dir/all] Error 2
make[1]: *** [CMakeFiles/test-app.dir/rule] Error 2
make: *** [test-app] Error 2

One way to link to another CMake project is to add it to your source tree (which you already did) and include in your CMake project with add_subdirectory command. In your case, you'll write

add_subdirectory(psmoveapi)

The subdirectory will inherit CMake variables from the containing directory, meaning that it will be built with the same compiler and settings, unless they get explicitly overridden. If you later find out that the project needs you to set up some CMake variables, you will set them before add_subdirectory .

Then, you can use all targets defined in that subdirectory. You'll need to find out which target corresponds to the library you want to link to, either by looking in the docs or by searching for add_library commands in the CMake scripts. That particular project seems to define a target called also psmoveapi , so you'll link to it like that:

target_link_libraries(your-program PRIVATE psmoveapi)

In principle, if the library target is set up properly, this will automatically take care of include paths, extra required libraries, etc, but in practice you may need to set them up youself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM