[英]how to include my library with angel brackets in cmake?
i am learning about Cmake and i am facing a problem, in my main project i use the Fmt library and i can include it using angle brackets but i can't use angle brackets with my own libraries in my learning project我正在学习 Cmake 并且我遇到了一个问题,在我的主项目中我使用 Fmt 库,我可以使用尖括号将其包含在内,但我不能在我的学习项目中使用尖括号和我自己的库
this is my directory这是我的目录
-build
-(contains building results)
-include
-zero
-zero.h
-zero.c++
-CmakeLists.txt
-main.c++
-CmakeLists.txt
zero/CmakeLists.txt:零/CmakeLists.txt:
cmake_minimum_required(VERSION 3.10)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
add_library(zero STATIC zero.c++ zero.h)
CmakeLists.txt: CmakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(MAIN)
add_subdirectory(${PROJECT_SOURCE_DIR}/include/zero)
set(EXECUTABLE_OUTPUT_PATH bin)
add_executable(main main.c++)
target_link_libraries(main include/zero)
message(${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(zero PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
main.c++: main.c++:
#include <iostream>
//#include <zero/zero.h> this gives an error when i compile it with mingw32-make:
// No such file or directory #include <zero/zero.h>
//#include <include/zero/zero.h> thie also gives the same error
int main() {
std::cout<<"hello world! ";
}
i am trying to learn how to make a library accessible via the <> syntax我正在尝试学习如何通过 <> 语法使库可访问
Edite:编辑:
i fixed it by changing the CmakeLists.txt to我通过将 CmakeLists.txt 更改为
cmake_minimum_required(VERSION 3.10)
project(MAIN)
add_subdirectory(${PROJECT_SOURCE_DIR}/include/zero)
set(EXECUTABLE_OUTPUT_PATH bin)
add_executable(main main.c++)
target_link_libraries(main zero)
message(${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(zero PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)
my first mistake was that target_link_libraries(main include/zero)
is wrong beacuse the second argumant should be the target name not the path我的第一个错误是
target_link_libraries(main include/zero)
是错误的,因为第二个参数应该是目标名称而不是路径
second i changed ${CMAKE_CURRENT_SOURCE_DIR}/include
to ${CMAKE_CURRENT_SOURCE_DIR}
and it worked i don't know the reason of it and this is my question know: why this worked?第二次我将
${CMAKE_CURRENT_SOURCE_DIR}/include
更改为${CMAKE_CURRENT_SOURCE_DIR}
并且它起作用了我不知道它的原因,这是我的问题知道:为什么它起作用?
Edite2:编辑2:
i changed ${CMAKE_CURRENT_SOURCE_DIR}
to ${CMAKE_CURRENT_SOURCE_DIR}/include
and changed <include/zero/zero.h>
to <zero/zero.h>
我将
${CMAKE_CURRENT_SOURCE_DIR}
更改为${CMAKE_CURRENT_SOURCE_DIR}/include
并将<include/zero/zero.h>
更改为<zero/zero.h>
this also worked but i don't know the reason of neither of them这也有效,但我不知道他们俩的原因
So I was really stupid and I didn't read the documentations correctly the reason that it works is what Tsyvarev said:所以我真的很愚蠢,我没有正确阅读文档,因为它起作用的原因是 Tsyvarev 说的:
You specify the project's root directory as an include directory.
您将项目的根目录指定为包含目录。 So
#include <include/zero/zero.h>
is able to include the fileinclude/zero/zero.h
in your project.因此
#include <include/zero/zero.h>
能够在您的项目中包含文件include/zero/zero.h
。When you specify the project's include subdirectory as an include directory, then you are able to include the same file via
#include <zero/zero.h>
.当您将项目的包含子目录指定为包含目录时,您可以通过
#include <zero/zero.h>
包含相同的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.