CMake将标题添加到Projectfile

时间:2014-02-09 03:25:06

标签: cmake

好的,我的root / CMakeLists.txt中有以下代码。

cmake_minimum_required(VERSION 2.8)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Debug or Release" FORCE)
endif()

if(CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configurations" FORCE)
    mark_as_advanced(CMAKE_CONFIGURATION_TYPES)
endif()

execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory install)
SET(CMAKE_INSTALL_PREFIX install)

project(efge)

# include
add_definitions(-DEFGE_EXPORTS)
include_directories("${CMAKE_SOURCE_DIR}/include/EFGE/"
            "${CMAKE_SOURCE_DIR}/include/EFGE/Math"
            "${CMAKE_SOURCE_DIR}/include/EFGE/System")

# preconf
set(EFGE_SHARED_LIBS TRUE CACHE BOOL "Shared libraries (needs shared SFML libraries, too)")

if(WIN32)
    set(EFGE_STATIC_STD_LIBS FALSE CACHE BOOL "Statically linked runtime/standard libraries? Must match with SFML-Option.")
    if(EFGE_STATIC_STD_LIBS)
        if(EFGE_SHARED_LIBS)
            message("\nEFGE_STATIC_STD_LIBS and EFGE_SHARED_LIBS aren\'t compatible!")
        elseif(MSVC)
            foreach(flag CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE)
                if(${flag} MATCHES "/MD")
                    string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
                endif()
            endforeach()
        elseif(CMAKE_COMPILER_IS_GNUCXX)
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
        endif()
    endif()
endif()

if(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "${CMAKE_CSS_FLAGS} -std=c++0x")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++")
    add_definitions(-DEFGE_USE_STD_RANDOMENGINE)
endif()

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules/;${CMAKE_MODULE_PATH}")

if(NOT EFGE_SHARED_LIBS)
    set(SFML_STATIC_LIBRARIES TRUE)
endif()
find_package(SFML 2 COMPONENTS audio graphics window system)

if(SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
else()
    set(SFML_ROOT "" CACHE PATH "SFML top-level directory")
    message("\n SFML directory not found. Please set SFML_ROOT to SFML top-level path, wich contains /include/ and /lib/ directories.")
    message("Make sure SFML libraries have the same configuration.")
endif()

# linking sfml
macro(efge_link_sfml EFGE_TARGET)
    target_link_libraries(${EFGE_TARGET} ${SFML_LIBRARIES})
endmacro()
macro(efge_link_sfml_dependencies EFGE_TARGET)
    target_link_libraries(${EFGE_TARGET} ${SFML_DEPENDENCIES})
endmacro()

macro(efge_link_efge EFGE_TARGET)
    target_link_libraries(${EFGE_TARGET} efge)
endmacro()

# source
add_subdirectory(src)

# include
install(DIRECTORY include
        DESTINATION .)

我的文件结构,如果这很重要,则如下:

+-- include
       +-- Math
            +--- *.hpp files
       +-- System
            +--- *.hpp files
       +--- *.hpp files
+-- src
     +--- *.cpp files
     +--- CMakeLists.txt
+--- CMakeLists.txt

如何将标题添加到生成的项目文件(例如CodeBlocks)? Makefile 似乎可以正常工作。像set_group这样的东西不会影响项目文件。 我无法弄清楚,第一次使用CMake。谢谢!

1 个答案:

答案 0 :(得分:1)

我刚检查了以下组合:

  1. CMake 2.8.12.1
  2. CodeBlocks 13.12
  3. CodeBlocks项目生成如下:

    mkdir build
    cd build
    cmake -G 'CodeBlocks - Unix Makefiles' ..
    

    我确实看到生成器生成了一个.cbp文件,其中列出了所有头文件。因此,您可以查看.cbp文件并检查是否列出了标题。如果他们不是,那么你应该检查你的CMake是否足够新鲜。

    更新

    啊,啊,我明白了。看来我已经用发电机再现了你的问题。这是最小的非工作示例:

    project/
       CMakeLists.txt
       src/
            CMakeLists.txt
            test.cpp
       include/test.h
    

    项目/的CMakeLists.txt:

    cmake_minimum_required(VERSION 2.8)
    
    project(TestProject)
    
    include_directories(include)
    
    add_subdirectory(src)
    

    项目/ SRC /的CMakeLists.txt:

    cmake_minimum_required(VERSION 2.8)
    
    project(TestProjectSources)
    
    set(SOURCES test.cpp)
    
    add_executable(test ${SOURCES})
    

    项目/ SRC / TEST.CPP

    #include "test.h"
    
    int main(void) {
        TestRun r;
        r.run();
        return 0;
    }
    

    项目/包含/ test.h:

    #ifndef _TEST_H_
    #define _TEST_H_
    
    #include <iostream>
    
    class TestRun {
    public:
        TestRun() { std::cout << "Hello World!" << std::endl; }
        void run() { std::cout << "Doing something useful" << std::endl; }
    };
    
    #endif // _TEST_H_
    

    在这种情况下,生成器生成没有任何头文件的TestProject.cbp。让我们看看如何解决这个问题。

    Update2 :好吧,我有一些坏消息要告诉你。不幸的是,负责搜索和添加头文件的CodeBlocks生成器的代码块太简单,无法正确处理您的案例。以下是来自CMake来源Source/cmExtraCodeBlocksGenerator.cxx的评论:

    // The following loop tries to add header files matching to implementation
    // files to the project. It does that by iterating over all source files,
    // replacing the file name extension with ".h" and checks whether such a
    // file exists. If it does, it is inserted into the map of files.
    // A very similar version of that code exists also in the kdevelop
    // project generator.
    

    因此,您可能会发现只有当test.cpptest.h并排放置在一个目录中时,此方法才有用。所以它是进一步改进的方向。我可以建议联系此论坛帖子http://forums.codeblocks.org/index.php?topic=16892.0中的人,并讨论如何改进这种情况。也许在项目生成中涉及gcc -MM -I<include directories> test.cpp调用是合理的,但是这种方法很昂贵,如果目前无法编译代码,可能很容易破解。