【问题标题】:Cmake Add new project to solutionCmake将新项目添加到解决方案
【发布时间】:2017-09-13 11:02:37
【问题描述】:

我是 cmake 的新手,所以如果问这个低问题但在谷歌上找不到答案,请原谅。

我正在尝试在 Ogre3d SDK 之上创建一个空项目。 我用 cmake 和 vs2015 构建了一个 ogre3d 源,现在我想在 ogre 库和头文件之上创建我的项目。如解释here 我创建了一个 cmakelists.txt 文件并将这些行添加到其中:

# specify which version you need
find_package(OGRE 1.10 REQUIRED)

project(OG1)

# the search paths
include_directories(${OGRE_INCLUDE_DIRS})
link_directories(${OGRE_LIBRARY_DIRS})

 # copy essential config files next to our binary where OGRE autodiscovers them
 file(COPY ${OGRE_CONFIG_DIR}/plugins.cfg ${OGRE_CONFIG_DIR}/resources.cfg 
  DESTINATION ${CMAKE_BINARY_DIR})

一切正常,成功创建 cmake 文件并使用 cmake-gui 生成 .sln 但是我希望 cmake 创建一个名为 OG1 的空项目,然后我可以运行我的第一个 ogre helloword,但它会生成仅包含 ALL_BUILD 和 ZERO_CHECK 的 .sln 文件。 如何创建一个具有所有配置并准备好使用 ogre 类的干净项目(我的意思是它会自动配置链接、包含等)?

【问题讨论】:

    标签: c++ visual-c++ cmake ogre


    【解决方案1】:

    CMake 术语与 Visual Studio 的术语略有不同。

    特别是,每个 CMake project 将创建一个 Visual Studio 解决方案(.sln 文件),而属于该 CMake 项目的所有 CMake targets 将在相应解决方案中显示为 Visual Studio 项目。

    CMake       Visual Studio
    
    project <-> Solution (.sln)
    Target  <-> Project (.vcxproj)
    

    所以您的项目显然缺少目标。在您的情况下,您可能需要一个使用add_executable 添加的可执行目标。可执行文件可以与项目同名。请注意,可执行文件在 CMake 中不能完全为空。虽然 Visual Studio 中存在空可执行项目的概念,但它在其他构建系统中无效,因此 CMake 也不支持它。所以这里至少需要指定一个源文件:

    add_executable(og1 og1.cpp)
    

    有了它,您还可以将剩余的构建选项附加到目标。这一步是可选的,因为您的代码也可以在这里工作,但在现代 CMake 中被认为是很好的风格:

    target_include_directories(og1 PUBLIC ${OGRE_INCLUDE_DIRS})
    target_link_libraries(og1 PUBLIC ${OGRE_LIBRARIES})
    

    请注意,在 CMake 中,we prefer to not use the library directory for telling the build system of the library's location,但始终使用库文件的完整路径。

    【讨论】:

    • 谢谢!很好的解释。它用于使用 cmake 创建 .sln 。我要写我的问候语。所以如果我有关于 Cmake 的问题,让我在这里问。和 1 个问题,为什么该网站不使用链接库?他们在需要调用包时这样做?
    猜你喜欢
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2010-09-18
    相关资源
    最近更新 更多