Reputation: 63
I have a CMake project with these relevant folders:
project_folder
-src
|--main.c
peripherals
|--something.h
|--something.c
My CMake in project_folder
includes:
add_subdirectory(peripherals)
if (NOT $ENV{TARGET_SOURCES} STREQUAL "")
target_sources(app PRIVATE $ENV{TARGET_SOURCES})
else()
target_sources(app PRIVATE src/main.c)
endif()
My CMake under peripherals
incudes:
add_library (peripherals something.c)
target_include_directories (peripherals PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
CMake under src
:
add_executable(app main.c)
target_link_libraries(app PRIVATE peripherals)
My project is building fully but when I try to include my header file in main.c
I get:
project_folder/peripherals/something.h:No such file or directory
In my main.c
file I have #include "peripherals/something.h"
. Does anyone know how to fix this? I'm not sure if my #include statement is correct and I think I am missing stuff in my CMakeLists files
Upvotes: 2
Views: 6770
Reputation: 39
in your CMake in project_folder:
include_directories(src)
and then in main.c:
#include <peripherals/i2c_test.h>
OR
in your CMake in project_folder:
include_directories(src/peripherals)
and then in main.c:
#include <i2c_test.h>
Upvotes: 0
Reputation: 238
Your include statement has to be relative to the included directory.
Edit: In your example ${CMAKE_CURRENT_SOURCE_DIR}
is the project directory (where your CMake file is).
Therefore you should be able to write:
#include <peripherals/something.h>
You can always check a cmake variable's content by printing it. In your CMake file you can write:
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})
When running cmake you should see the path printed in the console output.
Upvotes: 0
Reputation: 976
You'll need to define executable and "link" it with the library:
# project_folder/CMakeLists.txt
add_subdirectory(peripherals)
add_subdirectory(src)
# project_folder/src/CMakeLists.txt
add_executable(my_executable main.c)
target_link_libraries(my_executable PRIVATE peripherals)
Then, you'll need to include the header in main.c
properly- since you've linked against a library that includes peripherals
directory, you can now directly include it:
#include "something.h"
Upvotes: 2
Reputation: 9376
You can either do "#include "../peripherals/i2c_test.h"
in your main.cpp
OR
in your CMake in project_folder
:
target_include_directories(app ${CMAKE_CURRENT_SOURCE_DIR})
and then in main.c:
#include <peripherals/i2c_test.h>
....
Upvotes: 4