0

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

this is my directory

-build
   -(contains building results)
-include
   -zero
     -zero.h
     -zero.c++
   -CmakeLists.txt
-main.c++
-CmakeLists.txt

zero/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:

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++:

#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

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

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?

Edite2:

i changed ${CMAKE_CURRENT_SOURCE_DIR} to ${CMAKE_CURRENT_SOURCE_DIR}/include and changed <include/zero/zero.h> to <zero/zero.h>

this also worked but i don't know the reason of neither of them

mahdi tajik
  • 71
  • 1
  • 5
  • Does this answer your question? [C - Should I use quotes or brackets to include headers in a separate directory](https://stackoverflow.com/questions/65700648/c-should-i-use-quotes-or-brackets-to-include-headers-in-a-separate-directory) – BoP Jan 16 '22 at 13:35
  • @BoP no but thanks. i always use "" for the header files that i provided but i wanted to know how to make a library accessible with the <> syntax – mahdi tajik Jan 16 '22 at 13:38
  • `target_link_libraries(main include/zero)` You just use the name of the cmake target. The name of the target is `zero`, not `include/zero`. Furthermore imho the library-related info should be added in the `CMakeLists.txt` file creating the target, not in some other `CMakeLists.txt`. – fabian Jan 16 '22 at 13:42
  • @fabian sorry i am new to cmake i fixed it but i am still getting the same errors – mahdi tajik Jan 16 '22 at 13:47
  • "this also worked but i don't know the reason of neither of them" - You specify the project's root directory as an include directory. So `#include ` is able to include the file `include/zero/zero.h` in your project. When you specify the project's `include` subdirectory as an include directory, then you are able to include the same file via `#include `. Not sure what kind of other explanations you want. – Tsyvarev Jan 16 '22 at 15:32
  • @Tsyvarev thanks my problem was that I didn't read the documentations correctly so I faced problems – mahdi tajik Jan 16 '22 at 17:36

1 Answers1

1

So I was really stupid and I didn't read the documentations correctly the reason that it works is what Tsyvarev said:

You specify the project's root directory as an include directory. So #include <include/zero/zero.h> is able to include the file include/zero/zero.h in your project.

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>.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
mahdi tajik
  • 71
  • 1
  • 5