How can I specify additional include files?

I want to use the check C unit testing framework which is installed in /usr/local/include. How can I tell CLion/CMake to look in that directory for headers?

5
56 comments

According to the CMake documentation (http://www.cmake.org/cmake/help/v3.0/command/include_directories.html#command:include_directories) I can specify additional include directories with:

include_directories(/usr/local/lib)


CMake is able to build successfully using the library headers, but CLion highlights the include in red:

#include <check.h>


I guess the CLion's integration with CMake does not handle the include_directories directive. Is there a supported way to add additional libraries? Should I file a bug about include_directories?

4

I've also tried the following CMake directive, but the result is the same: CMake works fine but CLion highlights it in red and doesn't see the functions of the imported header:

set(CMAKE_INCLUDE_PATH /usr/local/lib)
2
Hello,
Why do you set /usr/local/lib to include_directories and not /usr/local/include?
include_directories(/usr/local/include) should work actualy, if it's not, please, fill the issue in our tracker with the sample project.
0

Because that's where homebrew installs libraries. Eventually I was able to solve my problem with build file below:

cmake_minimum_required(VERSION 2.8.4)
project(hello_clion)


# add extra include directories
include_directories(/usr/local/include)


# add extra lib directories
link_directories(/usr/local/lib)


# specify the executable (no libraries here)
add_executable(hello_clion hello_clion.c)


# specify the dependency on an extra library
target_link_libraries(hello_clion check.a)

0

Bordan, could you please try passing SYSTEM flag to 'include_directories' command?
Does it help?
If not, what C/CXX compilers are chosen in CMake | Cache toolwindow?

0

Hi everyone!
Yeah, i have this problem too...
I have a lot, and by lot, i mean LOT of include files in custom (fixed) locations around the system like $/HOME/local $Home/boost/include etc etc
CLions seems to omit this files in there is no possibility (at last i didn't found any) to point CLion to this directories. I cant confirm it because project that i am working on is huge, but also it seems to ignore even files in his own project directory, (that means a lot of inspections and other stuff like "ctrl+LMB->goto_definition/declaration" is generally not working as it should).
I assume that CLion keeps parsed information from files in some internal db for later use? In that case it may be some parsing rules or limit stuff? Or maybe it wont parse directory because there are symlinks in there?

JetBrains, please explain this magic! :3
Excellent work btw, i was waiting for stuff like this for looong time!
cheers!

0

Mateusz, does your project builds correctly if you try to build it with CMake manually? If it does not, then you need to correctly specify include paths in your CMakeLists.txt using "include_directories" command. For certain libraries, like boost, there are designated ways of specifying the library location, e.g. set(BOOST_ROOT "/path/to/boost").

0

I have no problems while building, As i said project is epicly huge and it is using few different building systems ( cmake, make bjam and others ). Running build is one thing, and CLion works perfectly in that case.
I have problems while editing projects, because CLion do not parse (internally) files not included in scope ( one scope on whole project ) and i cant use code completition, goto definition/declaration show uses and other fun functions.
I cant really point CLion to directory containing include files ( in project file i have somethong like #include <sals/memmap> or #include <ataxp> ) that are resolved by system or buildtool but are unknown by CLion.

0

Sorry, I just noticed that you mentioned that your include paths contain symlinks in your first post. I cannot be sure, but most likely that is what causing the problem. This issue was fixed recently and will be included into the next EAP build.

0

I still have the exact same problem that CLion cannot find fuse.h even though CMake works fine .

cmake_minimum_required(VERSION 2.8.4)
project(hello)

if(POLICY CMP0042)
  cmake_policy(SET CMP0042 NEW) # CMake 3.0
endif
()

LIST(APPEND CMAKE_MODULE_PATH "CMakeModules")

find_package(JNI)
find_package(FUSE)

if (JNI_FOUND)
    message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
    message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")
endif()

if (FUSE_FOUND)
    message (STATUS "FUSE_INCLUDE_DIRS=${FUSE_INCLUDE_DIRS}")
    message (STATUS "FUSE_LIBRARIES=${FUSE_LIBRARIES}")
endif()


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FILE_OFFSET_BITS=64")

set(SOURCES
    hello.c
)

add_library(hello SHARED hello.c)
include_directories(${JNI_INCLUDE_DIRS} ${FUSE_INCLUDE_DIRS})
target_link_libraries(hello ${JNI_LIBRARIES} ${FUSE_LIBRARIES})

0

I've got the same error.
Clion is not able to find header file by name on the windows-10 machine. But can find the header file by the relative path and name.
Cmake works fine. Compilation works on Mac OS X and Windows. Mac OS X version of Clion works well even with only name of file and understand where to look at header files. Windows version not.

```

cmake_minimum_required(VERSION 3.5.0)

project(STM23F103C8T6 C CXX ASM)

set(CMAKE_VERBOSE_MAKEFILE TRUE)

add_definitions(-DSTM32F103x6)

file(GLOB_RECURSE USER_SOURCES Src/*.c *.h)
file(GLOB_RECURSE HAL_SOURCES Drivers/STM32F1xx_HAL_Driver/Src/*.c)

add_library(CMSIS
${PROJECT_SOURCE_DIR}/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/system_stm32f1xx.c
${PROJECT_SOURCE_DIR}/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/startup_stm32f103xb.s)

include_directories(
Drivers/CMSIS/Device/ST/STM32F1xx/Include/
Drivers/CMSIS/Include/
Drivers/STM32F1xx_HAL_Driver/Inc/
Inc/)

add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${HAL_SOURCES} ${LINKER_SCRIPT})

target_link_libraries(${PROJECT_NAME}.elf CMSIS)

set(HEX_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT Building ${HEX_FILE} \nBuilding ${BIN_FILE})

```

0

I have it too. Has anyone found a solution yet?

1

Martin, can you compile your prj correctly?
Can you successfully run CMake command outside CLion on this prj?

0

Yes. I can both build the project within CLion and build externally with the CMake command . 

I made a new observation just now though:

I started a basic blinky example that I have made, but I forgot to point CLion to my toolchain file (-DCMAKE_TOOLCHAIN_FILE=toolchain.cmake). This puzzled me for a while because suddenly all the #include files stayed green and CLion seemed to be able to read them just fine. But of course the project wouldn't build without the toolchain file. Then when I included the toolchain file the #includes turned red and I got the same errors reported above again.

0

Martin, thanks for the investigation. Looks like this is related to the same problem as https://youtrack.jetbrains.com/issue/CPP-3962. 

0

Thanks a lot Anastasia. I will follow the progress there.

0
Avatar
Permanently deleted user

Removing the dot from end of the url, the link above is broken. check this: https://youtrack.jetbrains.com/issue/CPP-3962

0

Still not working if th include is defined in an included directory by :

add_subdirectory(SimpleTests)

in CMakeLists.txt in SimpleTests

include_directories(${PROJECT_SOURCE_DIR}/OpenCLManipulator ${PROJECT_SOURCE_DIR}/tests/googletest/googletest/include )

Don't work, CLion don't recognize as a include dir in the path.

Only workd if you put include_directories in the root CMakeLists.txt.

 

 

 

1

Ivan, does it compile?

0

CLion20180.1.1 - Issue still occurs:( 

3

Mitien, could you please submit a sample to our tracker? We'll check

0

@Anastasia, 

simple example uploaded https://youtrack.jetbrains.com/issue/CPP-12788

Thanks.

0

Clion 2018.2 Issue still occurs. When are you going to fix it?

include_directories does not work at all.

I can fix it if add directory path directly to add_executable(mytarget Include/Hardware ...)

In this case all includes in the files looks green but CLION can't regongnize types from these includes...

 

0

We currently try to reproduce on our side,  when we manage to do so we'll investigate the issue. We'll check your attachement to the issue soon

0

Hi!

Can I add an external includes directories only to the CLion's project without modification the CMakeLists.txt?

In my case, I can't build the project on the developer's machine, but I have most of the headers. Now I use vim/sublime with autocomplete plugins, but they aren't very well for navigation by source code.

1

Do you have these headers located under the project root or in some separate location?

0

They are in a separate location.

0

For CMake projects, CLion need an ability to run CMake correctly on the project in order to open it. If you have your header files somewhere, then probably CMake can't finish correctly for your project. Then you have to update CMakeLists.txt with the corresponding include directories.

As an alternative, you can generate compilation database out of CMake on the machine where you can run it correctly and then open the project in CLion on another machine via a compilation database.

0

Ok, thank you. Unfortunatly, it doesn't suite me. To have the same path on both machines is a bit hard.

0

I see two solutions then:

  1. Symlink
  2. A Variable in CMake which contains the include_directory path, that has some default values probably and also you can update it via CMake parameters, which in case of CLion you can pass in CMake Profile options: Settings/Preferences | Build, Execution, Deployment | CMake
0

Please sign in to leave a comment.