[英]How to include files in a subdirectory of a CMake project, without using another CMakeLists.txt
I have a project added as a submodule to another project using CMake.我有一个项目作为子模块添加到另一个使用 CMake 的项目。 The submodule is just two files ( bc
, bh
) so the project structure is:子模块只有两个文件( bc
, bh
)所以项目结构是:
root/
| CMakeLists.txt
| main.cc
| a.cc
| a.h
| submodule/
| | b.c
| | b.h
I don't want to add another CMakeLists.txt to the submodule as it's just another git thing to maintain.我不想向子模块添加另一个 CMakeLists.txt,因为它只是另一个需要维护的 git 东西。 The submodule will never grow beyond these two files so I just want something simple and CMake is overkill for some of my other projects, so I don't want it in the submodule's git repo.子模块永远不会超出这两个文件,所以我只想要一些简单的东西,而 CMake 对于我的其他一些项目来说太过分了,所以我不希望它出现在子模块的 git 仓库中。
If I try include_directories(submodule)
and add submodule/bc
to the sources list of my add_executable
block, I get undefined references to the functions inside bc/h
如果我尝试include_directories(submodule)
并将submodule/bc
添加到我的add_executable
块的源列表中,我会得到对bc/h
中函数的未定义引用
Is there a quick and simple way to just add these extra files to the sources without getting undefined references?有没有一种快速简单的方法可以将这些额外的文件添加到源中而不会得到未定义的引用? Or is there a better way of managing this tiny library?或者有更好的方法来管理这个小图书馆吗?
I have我有
include_directories(${PROJECT_SOURCE_DIR}/submodule)
...
add_executable(${NAME}
submodule/b.c
main.cpp
server.cc
)
I have also tried adding我也试过添加
target_link_directories(${NAME} PUBLIC
${PROJECT_SOURCE_DIR}/submodule
)
The error is:错误是:
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: CMakeFiles/light.dir/
server.cc.obj: in function `tcpRecvCallback(void*, tcp_pcb*, pbuf*, signed char) [clon
e .part.0]':
server.cc:(.text._Z15tcpRecvCallbackPvP7tcp_pcbP4pbufa.part.0+0x62): undefined referen
ce to `http_req_parse(char*)'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/light.dir/build.make:3083: light.elf] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:1520: CMakeFiles/light.dir/all] Error 2
gmake: *** [Makefile:156: all] Error 2
where server.cc is in the project root, and http_req_parse
is a function in bc
其中server.cc在项目根目录下, http_req_parse
是bc
中的一个函数
main.cpp
includes server.h
which includes submodule/bh
main.cpp
包含server.h
,其中包含submodule/bh
It may be relevant that I am building for a raspberry pi pico W.我正在为 raspberry pi pico W 构建可能是相关的。
new answer:新答案:
include_directories(${PROJECT_SOURCE_DIR}/submodule)
set(SOURCES submodule/b.c)
add_executable(${PROJECT_NAME} SOURCES)
old answer:旧答案:
include_directories(${PROJECT_SOURCE_DIR}/submodule)
file(GLOB submodule_source submodule/*.c)
add_executable(${PROJECT_NAME} submodule_source)
It turns out that all the above answers were correct, I was forgot to use the language specific guards, since my library was in C, and my project was in C++.事实证明,以上所有答案都是正确的,我忘记使用特定语言的守卫,因为我的库是用 C 编写的,而我的项目是用 C++ 编写的。
IE, I didn't have即,我没有
#ifdef _cplusplus
extern "C" {
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.