-3

I'm trying to use GLOB_RECURSE to specify my sources and headers files. Currently, my CMakeLists.txt for a static library is:

project(LinearSystemLib)

file(GLOB_RECURSE ${PROJECT_NAME}_headers ${PROJECT_SOURCE_DIR}/*.h)

file(GLOB_RECURSE ${PROJECT_NAME}_sources ${PROJECT_SOURCE_DIR}/*.cpp)

add_library(

${PROJECT_NAME} STATIC ${${PROJECT_NAME}_headers} 

${${PROJECT_NAME}_sources}

)

install(

    TARGETS ${PROJECT_NAME}

    LIBRARY DESTINATION libs

    ARCHIVE DESTINATION archives

)

The library directory looks like this:

LinearSystemLib

           CMakeLists.txt

           source

                LinearSystemLib.cpp

            include

                LinearSystemLib.h

When I run command cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug (in the build directory) everything goes ok. Yet, command make it displays the following:

/home/felipe/Documents/Dados/SINMEC/Eclipse/LinearSystemLib/source/LinearSystemLib.cpp:1:29: fatal error: LinearSystemLib.h: No such file or directory

Is my CMakeLists wrong? I don't want to set specify the sources and headers files by name. I'm not finding information about glob_recurse easily.

I can make it work by listing the sources and headers files by name. However, it MUST be done with the glob_recurse or with glob.

  • 3
    Possible duplicate of [How to properly add include directories with CMake?](http://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake) – m.s. Nov 05 '16 at 14:26
  • See [CMake/Examples: Recursively add directories to INCLUDE_DIRECTORIES](http://www.vtk.org/Wiki/CMake/Examples#Recursively_add_subdirectories_to_INCLUDE_DIRECTORIES) – Florian Nov 07 '16 at 16:01
  • Please don't add words like "solved" to your question title, as that makes it no longer a question. If an answer has solved your problem, mark it as accepted. – Ryan Bemrose Nov 07 '16 at 20:01

2 Answers2

2

I solved my problem, here's what LinearSystemLib directory looks like:

LinearSystemLib

       CMakeLists.txt

       source

            LinearSystemLib.cpp

        include

            LinearSystemLib.h

The CMakeLists.txt contains:

project(LinearSystemLib)


#INCLUDE DIRECTORIES
include_directories(${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/include)


#SEARCH FOR .CPP AND .H FILES
file(GLOB ${PROJECT_NAME}_headers ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/include/*.h)
file(GLOB ${PROJECT_NAME}_sources ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/source/*.cpp)


#ADD LIBRARY
add_library(${PROJECT_NAME} STATIC ${${PROJECT_NAME}_sources})


#DEFINE OUTPUT LOCATION
install(
    TARGETS ${PROJECT_NAME}
    ARCHIVE DESTINATION static_libs
)

You don't actually NEED to add the .h/.hpp files using GLOB. I did it because otherwise, Visual Studio (or CodeBlocks) wouldn't create a "Header Files" folder on the project menu.

I wasn't properly specifying the path where GLOB would find the files.

${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/source/ 
  • Could you please elaborate why you need the additional `CMakeLists.txt` file in the `source` subdirectory? I seems unusual. You could just have added a `include_directories(include)` call in the your main CMakeLists.txt file. Relative paths are sufficient, CMake will prefix [`CMAKE_CURRENT_SOURCE_DIR`](https://cmake.org/cmake/help/latest/variable/CMAKE_CURRENT_SOURCE_DIR.html) automatically. – Florian Nov 07 '16 at 20:11
  • Hello, I have several include directories in this project. I don't know if your suggestion still applies then. – Felipe Giacomelli Nov 07 '16 at 20:30
  • Actually, there is no need for the CMakeLists.txt in the source subdirectory. You can have simply one .txt file in the project directory. – Felipe Giacomelli Nov 12 '16 at 00:41
0

You need to add

target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR})

so the correct -I argument is added to your compilation step. Use make VERBOSE=1 to see exactly what commands make is executing.

Phil
  • 5,822
  • 2
  • 31
  • 60
  • Hello. I tried to include, however I have gotten the same error. Would you have CMakeLists.txt file which you can share? – Felipe Giacomelli Nov 05 '16 at 18:24
  • I do not have one that will correctly build your code. If you run ``make VERBOSE=1``, that should help you see what ``-I`` argument(s) are being used and how to adjust the ``target_include_directories`` command so it works. If not, you will need to post your code somewhere, so someone can review it and see exactly what is going wrong. – Phil Nov 07 '16 at 13:04