add root directory as include directory - c++

During compilation certain header files aren't found, although I added the respective include directories in CMake. Unfortunately this is the code and directory structure I am stuck with and I can't change anything on the include statements.
I have the following directory structure:
projectfolder
+--source1
| |--prog.cpp
| |--anotherHeader.h
| |--CMakeLists
|
+--includefolder
| +--source1_include
| | |--header.h
|
|--CMakeLists
CMakeLists in projectfolder looks like this:
project (project)
include_directories(includefolder)
add_subdirectory(source1)
prog.cpp has:
#include "source1_include/header.h"
and header.h has:
#include "anotherHeader.h"
(don't ask me why, I don't know myself, maybe it has something to do with the fact that originally this is a Visual Studio project)
I thought I could fix this, by adding
include_directories(.)
to the CMakeLists in the source1 directory but unfortunately it wont work that way. anotherHeader.h isn't found.

I'm assuming that "." is "projectfolder"
If I understand correctly, you added -I "projectfolder", so now "source1_include/header.h" correctly finds "projectfolder/source1_include/header.h"
Now, "header.h" tries to include "anotherHeader.h", which is not in its folder and not in any of the included folders. It is actually in "source1". So cmake is correct to error out.
You need to either move "anotherHeader.h" into your includes folder (my recommendation), or edit "header.h" to find it by the correct relative path "../source1/anotherHeader.h" (not recommended), or add include_directories("source1"), which is where it actually is.

Related

CMake target_include_directories does not affect header files

My project is roughly structured like this:
├CMakeLists.txt
|
├───ExampleApp
| ├───CMakeLists.txt
| ├───header.hpp
| └───main.cpp
|
└───ExampleLibrary
├───CMakeLists.txt
├───mylib.hpp
└───mylib.cpp
In the root CMakeLists.txt I call
add_subdirectory(ExampleLibrary)
add_subdirectory(ExampleApp)
To build the library I call:
add_library(ExampleLibrary
mylib.hpp mylib.cpp
)
And finally, in the executable, I try to do:
add_executable(ExampleApp
header.hpp main.cpp
)
target_include_directories(ExampleApp
PRIVATE ${PROJECT_SOURCE_DIR}/ExampleLibrary
)
target_link_libraries(ExampleApp
Path/To/The/Binary/Directory
)
Now the build files generate just fine, and the project also builds with no errors. However, when I now try to include mylib.hpp in header.hpp, I get build errors because it can't find the file mylib.hpp. But I actually can include mylib.hpp in main.cpp and the project builds and compiles.
Am I missing something? I thought target_include_directories() works for both .cpp and .hpp files.
It seems like you may not have added the correct directory as an include directory for the ExampleApp. The PROJECT_SOURCE_DIR variable evaluates to the directory in which the last project() call was made in your CMake project. You have not shown where this is, but you could use the root directory of the CMake project to be sure it is correct; try using ${CMAKE_SOURCE_DIR}/ExampleLibrary in the target_include_directories call instead:
target_include_directories(ExampleApp
PRIVATE ${CMAKE_SOURCE_DIR}/ExampleLibrary
)
A couple more notes:
If you aren't using an IDE such as Visual Studio for compilation, there is no need to add header files to calls like add_library() and add_executable(). Doing this only ensures they are shown in an IDE.
Instead, specify directories within which the headers can be found, using target_include_directories(). In your case, it sounds like you should have both directories listed here.
To link the ExampleLibrary target to the executable, you can simply use the target name in target_link_libraries(). There is no need to list out the binary directory (unless you are linking other libraries from there).
So with this in mind, the ExampleApp CMake file could look something like this:
add_executable(ExampleApp
main.cpp
)
target_include_directories(ExampleApp PRIVATE
${PROJECT_SOURCE_DIR}/ExampleLibrary
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(ExampleApp PRIVATE
ExampleLibrary
)

How to set up sub directory includes in CMake/CLion?

This is similar to Including directories in Clion, but after following the accepted answer there I made no progress.
I'm trying to edit a large OSS project in CLion. It does not use CMake, so CLion has generated a CMakeLists.txt file. When I open a source file, it is unable to resolve includes that use sub directories:
The file this screenshot is from is in the same "opto" subdirectory it is importing from. If I change the imports to not include "opto" it works fine, but I can't do that, since this is a major project and I'm just wanting to write a small patch:
$ find . -type f | wc -l
10532
I've added the file I'm importing directly to add_executable as suggested in the other answer:
# CMakeLists.txt
add_executable(hotspot
[lots of other files]
src/share/vm/opto/compile.hpp
)
And I've added the opto directory to include_directories as outlined in the second answer to the other question:
# CMakeLists.txt
include_directories(
src
src/share/vm/opto)
Neither is helping CLion resolve imports via the opto subdirectory.
What am I missing?
Using include_directories() must do, but you have to mention each sub_directory separately. Below i have included two directories in the same way where one of it is sub_directory.
The Header files have successfully been detected by CLion.

Include files in CLion

I have the following problem:
I try to start coding C++ for a little project in my company. Therefore I received a precoded program. Since I am new to C++ i thought about using a IDE, CLion in my case. The precoded program was build with vim at the console (I don't know if this is the reason for the following, I guess building/linking, problem)
My folder/file structure looks like this:
src
|_subprogramA
|_src
|_main.hpp
|_main.cpp
|_ ...
|_subprogramB
|_ ..
|_src
|_...
...
|_libhttp
|_src
|_http.hpp
|_http.cpp
|_ ..
CMakeList.txt
In my CMakeList.txt, the paths to all .cpp and .hpp files are set by set(Source_Files ) add_executables(projectname ${SOURCE_FILES})
When i build subprogramA, i get the errormessage: fatal error: http/http.hpp: No such file or directory. It is included by #include .
Why does the compiler not find the http.hpp file in libhttp/src/http although it is specified in CMakeList? Is there a way to tell it where it is located except changing the "include" to the full path: #include libhttp/src/http.hpp or #include ../../lib.. and so on? (I do not wanna change this codepath since it is used by other persons in another company)
I wounder that i can run the tool via console but i cant build it with clion IDE.
Best regards, I am happy for every help. And sorry if the question is kinda stupid but starting c++ with a kinda big precoded tool without IDE seems to be impossible for me ^^
Frank :)

CMake inlucde project file without folder prefix

My project structure is the following:
-Engine
|-Shaders
| '-shader.glsl
|-source
| |-Math
| |'- matrix.hpp
| | - vector.hpp
| '-Lua
| |'-lua_state.hpp
'-CMakeLists.txt
I am using CMake as build system and my question is :
How can I include for example matrix.hpp into lua_state.hpp without using the prefix for folder path ? i mean instead to use in lua_state.hpp #include "../Math/matrix.hpp" , just included like this -> #include "matrix.hpp" , there is any way to do this with CMake ?
You can specify the include path for compiler (-I option)
To include it for all targets (docs)
include_directories(source/Math)
or use target properties (docs)
target_include_directories(yourAppOrLibTargetName PRIVATE source/Math)
Add the directory to the include path by calling the include_directories command.
You'll also need to make sure the header files are declared as dependencies of the library or executable you're creating, either using add_library or add_executable. I can't quite tell what you'll need based on the files you listed.

Describing header file locations in makefile

In a new project I am working on I have the following dir structure:
Project_base
|---- src
|---- bin
|---- h
| Makefile
And in my source files I have includes that look like so:
#include "../h/SomeHeaderFile.h"
instead of the more correct form:
#include "SomeHeaderFile.h"
What do I need to add to my makefile so that I can removed the relative path includes so they will look normal?
ADDITION: also, where do I set this in CDT (C++ for eclipse) so that in design time this is reflected as well?
You need to add -I../h in the list of parameters you pass to gcc.
Add a flag to your CFLAGS so that the root of the headers is part of the headers search path:
-IProject_base/h
That way gcc/g++ will also look here in addition to the default headers directories.