【发布时间】:2022-01-18 04:24:25
【问题描述】:
希望有人能帮忙
我的结构如下:
Top_dir
--> CmakeLists.txt
-->include
----> defs.h (access the functions in static library)
----> moredefs.h (access the functions in static library)
----> myClass.h (Header file of my class - includes the defs.h and moredefs.h)
-->lib
---->src
------> functions.c (autogenerated - includes the defs.h and moredefs.h)
----> libsomelib.a (given to me with a compiler to make the auto generated headers)
-->src
----> main.cpp
----> myClass.cpp
我的 cmakelists 造成困扰 - 我不知道如何编译以将 libsomelib.a 和 c 文件包含到库中并添加到主可执行文件中。
project(myProject)
set(MODULE_NAME ${PROJECT_NAME})
set(LIB_NAME ${MODULE_NAME})
########### SETUP #####################
find_package(catkin REQUIRED COMPONENTS
roscpp
)
set(MAIN
src/main.cpp
)
set(SOURCES
src/myClass.cpp
)
set(HEADERS
include/myClass.h
include/defs.h
include/moredefs.h
)
############ LIB ########################
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_library(${LIB_NAME}
${SOURCES}
${HEADERS}
)
add_library(myAttemptLib
lib/src/functions.c
#include/defs.h
#include/moredefs.h
)
#add_library(myAttemptLib STATIC IMPORTED)
set_target_properties(myAttemptLib PROPERTIES IMPORTED_LOCATION lib/libsomelib.a)
target_link_libraries(${LIB_NAME}
${catkin_LIBRARIES}
)
############ EXE ########################
add_executable(${PROJECT_NAME}_node ${MAIN})
add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(${PROJECT_NAME}_node
${LIB_NAME}
${catkin_LIBRARIES}
myAttemptLib
)
这将编译并运行。我可以从头文件中添加和使用数据类型没问题,它将编译并且代码运行,但是一旦我尝试使用包含在 libsomelib.a 中的函数,那么编译错误未定义对函数和配方的引用目标失败。
在一个小测试区域的 make 文件中,我可以编译并生成独立运行的可执行文件。
我觉得我应该用主 exe 编译 .a + .c 的某种方式,但是已经搜索了很多并且没有尝试在这里接触。 我希望有人可以提供帮助。
干杯
史蒂夫
【问题讨论】:
-
使用
target_link_libraries命令执行与预构建库的链接,方法是使用完整库路径或使用带有属性IMPORTED_LOCATION的导入目标包含完整库路径。在duplicate question 及其答案中查看更多信息。
标签: c++ cmake compiler-errors static