做模具的網(wǎng)站seo智能優(yōu)化
1 簡介
這篇文章將探討了在使用CMake構(gòu)建C++項目時,調(diào)用set_target_properties函數(shù)時參數(shù)數(shù)量不正確所引發(fā)的問題。
2 錯誤案例
以下為可能發(fā)生錯誤的案例
include_directories (${CMAKE_SOURCE_DIR}/common)
find_package(Threads)add_library (libusbmuxd SHARED libusbmuxd.c sock_stuff.c ${CMAKE_SOURCE_DIR}/common/utils.c)
find_library (PTHREAD pthread)
target_link_libraries (libusbmuxd ${CMAKE_THREAD_LIBS_INIT})# 'lib' is a UNIXism, the proper CMake target is usbmuxd
# But we can't use that due to the conflict with the usbmuxd daemon,
# so instead change the library output base name to usbmuxd here
set_target_properties(libusbmuxd PROPERTIES OUTPUT_NAME usbmuxd)
set_target_properties(libusbmuxd PROPERTIES VERSION ${LIBUSBMUXD_VERSION})
set_target_properties(libusbmuxd PROPERTIES SOVERSION ${LIBUSBMUXD_SOVERSION})install(TARGETS libusbmuxdARCHIVE DESTINATION lib${LIB_SUFFIX}LIBRARY DESTINATION lib${LIB_SUFFIX}
)
install(FILES usbmuxd.h usbmuxd-proto.h DESTINATION include)
以上文件可能報錯如下:
CMake error at CMakeLists.txt:12 (set_target_properties):set_target_properties called with incorrect number of arguments
3 原因分析
set_target_properties 函數(shù)的語法格式為
SET_TARGET_PROPERTIES(target1 target2 ... targetMPROPERTIES prop1 val1 prop2 val2 ... propN valN
)
變量LIBUSBMUXD_VERSION和LIBUSBMUXD_SOVERSION未定義,因此命令的語法是
SET_TARGET_PROPERTIES(target PROPERTIES name value)
很顯然,這里少了value變量
4 解決方法
要解決這個問題,請嘗試引用變量;使用“$ {LIBUSBMUXD_SOVERSION}”應(yīng)確保即使變量未定義,它也會采用空字符串的值,從而遵守語法。
include_directories (${CMAKE_SOURCE_DIR}/common)
find_package(Threads)add_library (libusbmuxd SHARED libusbmuxd.c sock_stuff.c ${CMAKE_SOURCE_DIR}/common/utils.c)
find_library (PTHREAD pthread)
target_link_libraries (libusbmuxd ${CMAKE_THREAD_LIBS_INIT})# 'lib' is a UNIXism, the proper CMake target is usbmuxd
# But we can't use that due to the conflict with the usbmuxd daemon,
# so instead change the library output base name to usbmuxd here
set_target_properties(libusbmuxd PROPERTIES OUTPUT_NAME usbmuxd)
set_target_properties(libusbmuxd PROPERTIES VERSION " ${LIBUSBMUXD_VERSION}")
set_target_properties(libusbmuxd PROPERTIES SOVERSION " ${LIBUSBMUXD_SOVERSION}")install(TARGETS libusbmuxdARCHIVE DESTINATION lib${LIB_SUFFIX}LIBRARY DESTINATION lib${LIB_SUFFIX}
)
install(FILES usbmuxd.h usbmuxd-proto.h DESTINATION include)