gpt4 book ai didi

CMake add_subdirectory 与 find_package?

转载 作者:太空宇宙 更新时间:2023-11-03 23:45:57 24 4
gpt4 key购买 nike

我有以下项目/源结构:

MyProject
|-MyProject
| |-src
| | |-project.c
| |
| |-CMakeLists.txt
|
|-MyLibrary
|-include
| |-hello.h
|
|-src
| |-hello.c
|
|-CMakeLists.txt

MyProject/src/project.c 包含 MyLibrary/include/hello.h,后者又包含来自系统的 libusb.h (使用 CMake 找到)。

我正在使用以下代码在 MyLibrary/CMakeLists.txt 中添加 libusb 依赖项:

# Find libusb
find_package(LibUsb REQUIRED)

# Include libusb
include_directories(${LIBUSB_INCLUDE_DIR})

# Add dependencies
target_link_libraries(owi535 ${LIBUSB_LIBRARY})

然而,在编译 MyProject(包括使用 add_subdirectorytarget_link_librariesMyLibrary)时,我得到一个错误说明libusb.h 找不到。

我可以自己编译 MyLibrary,但是编译 MyProject 需要 libusb.h 在它的包含路径中,而它不是.

有没有办法通过添加 MyLibrary 作为依赖项,MyProject 通过它拉动 libusb.h?这意味着我不需要为包含 MyLibrary 的每个项目重复 find_package 代码。


我遇到的另一个问题是相似的;当我编译 MyProject 时,在 MyLibrary 中调用 libusb 函数时出现错误,指出存在 Undefined symbols,但是在编译 MyLibrary 时 它自己,没有错误。

Undefined symbols for architecture x86_64:
"_libusb_close", referenced from:
_my_close_method in libMyLibrary.a(hello.c.o)
"_libusb_exit", referenced from:
_my_exit_method in libMyLibrary.a(hello.c.o)
"_libusb_init", referenced from:
_my_init_method in libMyLibrary.a(hello.c.o)
"_libusb_open_device_with_vid_pid", referenced from:
_my_open_method in libMyLibrary.a(hello.c.o)
ld: symbol(s) not found for architecture x86_64

最佳答案

来自cmake documentation :

PUBLIC and INTERFACE items will populate the INTERFACE_INCLUDE_DIRECTORIES property of <target>.

Targets may populate this property [INTERFACE_INCLUDE_DIRECTORIES] to publish the include directories required to compile against the headers for the target. Consuming targets can add entries to their own INCLUDE_DIRECTORIES property such as $<TARGET_PROPERTY:foo,INTERFACE_INCLUDE_DIRECTORIES> to use the include directories specified in the interface of foo.

因此,您需要在 MyLibrary/CMakeLists.txt 中使用它:

target_include_directories(MyLibrary PUBLIC ${LIBUSB_INCLUDE_DIR})

关于CMake add_subdirectory 与 find_package?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33697505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com