How to add CMake includes and libraries to Visual Studio Solution?

26,346

Solution 1

CMake is pretty well documented, if I have understood your question then the commands I think you're looking for are

Although some configuration is done by setting variables, most of it is done using commands to add certain information to parts of the build and slightly less frequently by setting properties on targets.

Solution 2

I believe that include_directories ("path") somewhere in the CMakeLists.txt does add the path to C++ includes path.

Share:
26,346
Hellhound
Author by

Hellhound

Software developer for JAVA/C++.

Updated on August 01, 2022

Comments

  • Hellhound
    Hellhound almost 2 years

    I use CMake to generate a Visual Studio 2010 project and solution file. Actually I could set different settings, like warning level, incremental building flag ect. from CMake. But I can't set additional includes and libraries, listed in the VC++ Directory configuration tab. Actually I've to set up those directories manually. This is stupid and boring...

    I tried to set the following CMake variables: CMAKE_INCLUDE_PATH, INCLUDE_DIRECTORY but nothing happend. If i open the project, the additional include directory of the solution is always empty (only standard MSVE settings are given).

    I also tired to set this variables after executable creation, but this has also no effect.

    This is what i do directly in the header of the cmake file:

    CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
    PROJECT(${MODULE_NAME})
    IF (MSVC)
       # Activate C++ exception handling
       IF (NOT CMAKE_CXX_FLAGS MATCHES "/EHsc")
       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") 
       ENDIF ()
    
       # Set Warning level always to 4
       IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
         string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
       ELSE ()
         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
       ENDIF ()
    
       #read path of dependency modules  
       file(READ "msvc.deps" MSVC_PROPERTIES)
       STRING(REGEX REPLACE ";" "\\\\;" MSVC_PROPERTIES "${MSVC_PROPERTIES}")
       STRING(REGEX REPLACE "\n" ";" MSVC_PROPERTIES "${MSVC_PROPERTIES}")
    
       FOREACH(e ${MSVC_PROPERTIES})
         SET(INCLUDE ${INCLUDE} ${e})
         MESSAGE(STATUS "[INFO]: Value ${e}")
       ENDFOREACH(e)
       INCLUDE_DIRECTORIES(${INCLUDE})
    ENDIF ()
    

    In the .deps file I've added to path of my dependeny modules, line separated:

    c:\binrev\development\boost\1.47\includes
    c:\binrev\repository\modules\brCore\trunk\includes
    

    Both are read successfully but couldn't be set as additional include directory in my MSVC solution.

    Best regards, Hellhound

  • Hellhound
    Hellhound over 12 years
    I know that CMake is well documented but this variables are not working. I set any of those variables, but when I open the MSVC solution nothing is set.
  • Hellhound
    Hellhound over 12 years
    I also tried to set the MSVC environment variables INCLUDE and PATH with SET(ENV${PATH} "c:\test\...\") but this also has no effect. The directories of the MSVC solution are empty.
  • Hellhound
    Hellhound over 12 years
    Those variable is set, but nothing is happend. In the solution the includes directory is empty. I also tried to set this variable after the exectuable generation, but no effect.
  • Adam Bowen
    Adam Bowen over 12 years
    They are not set even after adding include_directories(/path/to/include) to your CMakeLists.txt file? Are you adding the include_directories BEFORE you declare your target? (The order matters). If so can you post some or all of your CMakeLists.txt file illustrating the problem? Note that the CMAKE_INCLUDE_PATH variable is unrelated to the include path used by the compiler and there is no variable or command called INCLUDE_DIRECTORY.
  • Hellhound
    Hellhound over 12 years
    Yes nothing is set in the additional include directory of the MSVC solution after calling include_directories(/path/to/include). And yes the include directories are set on top before i declare the targets.
  • Adam Bowen
    Adam Bowen over 12 years
    Does the project compile anyway? Unfortunately I don't have access to Visual Studio at the moment to experiment, but I know that our build system makes extensive use of include_directories and that the whole thing builds happily as a visual studio project. It might be that CMake adds the settings to some other part of the project.
  • Hellhound
    Hellhound over 12 years
    For GCC anything is compiled and linked succesfully. For MVSE I've to set those directories manually in the solution file. If I've set those variables manually anything is build and compile fine... I've added some CMake snipped to my question.
  • Adam Bowen
    Adam Bowen over 12 years
    I managed to get 5 minutes to check the code you posted. If I add add_executable( test test.cpp) to the end of the configuration file and generate a VS2010 project (that's the version we have), then right clicking on the test project, properties, then go to C/C++ --> General then the include directories are listed under the Additional Include Directories property. If this isn't the case for your project then there must be something wrong outside of the code you have posted.
  • Adam Bowen
    Adam Bowen over 12 years
    I would advise creating a minimal CMakeLists.txt file and see what the minimum amount of code you need to reproduce the problem is. I should note we use (and require) CMake 2.8.
  • Hellhound
    Hellhound over 12 years
    Ahh ok. I see the failure. You are right, those files are added, but to an other tab at the solution. I've checkedd poperties -> VC++ Directories --> Include Directories. Is it possible to set any required value here (includes, libraries)?
  • Adam Bowen
    Adam Bowen over 12 years
    I don't think so, I'm not terribly familiar with VS2010 - are those settings "global" to all projects, if so CMake would never add project specific stuff there.