设置基本的多平台cmake启用项目时需要帮助(Help needed while setting up basic multiplatform cmake

编程入门 行业动态 更新时间:2024-10-25 06:32:53
设置基本的多平台cmake启用项目时需要帮助(Help needed while setting up basic multiplatform cmake-enabled project)

tl; dr问题在底部。

我是一个尝试新事物的开发人员 - 我的最后一个毒药是c ++。 由于我将一半的时间花在我的Linux笔记本电脑上而另一半花在Win XP PC上,我试图找到一种方法来创建基本的准系统项目,使用良好的c ++实践(好吧,我从经验中不了解它们,我刚看过他们)。 现在我的项目几乎可以在使用cmake . && make cmake . && make on linux(当标题和源文件在同一个文件夹中时它会起作用,当我将它们分成include / src文件夹时失败)。 我正在使用nuwen在windows上分发mingw(我知道工具链正在工作,它在Eclipse中编译项目没有任何问题)。

我的项目目录如下所示:

engine | |- main | |- include | |- App.h |- CMakeLists.txt (2) |- src | |- main.cc |- App.cc |- CMakeLists.txt (3) |- CMakLists.txt (1)

文件的内容非常简单(为了清楚起见,我将删除包含警卫等)

App.h:

class App { public: App(); int onExecute(); };

App.cc:

#include <iostream> #include "App.h" App::App() { } int App::onExecute() { std::cout << "inside app.." << '\n'; return 0; }

main.cc:

#include <iostream> #include "App.h" using namespace std; int main(int argc, char* argv[]) { App a; a.onExecute(); std::cout << "inside main.." << '\n'; }

CMakeLists.txt(1) - 主要的一个:

cmake_minimum_required (VERSION 2.6) set (CMAKE_CXX_COMPILER "g++") project (gameengine) add_definitions ( "-Wall -ansi -pedantic") add_subdirectory (${CMAKE_SOURCE_DIR}/main/include) add_subdirectory (${CMAKE_SOURCE_DIR}/main/src) add_executable (engine ${CMAKE_SOURCE_DIR}/main/src/main.cc) target_link_libraries (engine Application)

CMakeLists.txt(2) - 在include目录中

add_library (Application App) set_target_properties (Application PROPERTIES LINKER_LANGUAGE CXX)

CMakeLists.txt(3) - 在src目录中

include_directories (../include)

这是我得到的 - 有一些变化(即将App.cc移动到include目录)整个事情编译并在linux上运行良好 - 但我无法让mingw生成器在Win XP上运行。 我手动调整文件CMakeCache.txt中的CMAKE_MAKE_PROGRAM以指向正确的make.exe(我知道这应该被定义为系统变量但是因为我在许多不同的PC上工作,我不想留下垃圾后我)。

我的问题是:

1)编写多平台CMakeLists.txt文件(它将独立于os和项目文件的位置工作)的指导原则是什么,这最好能让我轻松地从一个操作系统配置另一个操作系统?

2)如何解决未找到头文件的错误(make give:(...)\ engine \ main \ src \ main.cc:2:17:致命错误:App.h:没有这样的文件或目录)?

感谢您的时间和帮助。

tl;dr the questions are at the bottom.

I am a developer trying something new - my last poison is c++. As I am spending half of my time on my linux laptop and the other half on Win XP PC, I tried to find a way to create basic, barebone project, using good c++ practices (well, I don't know them from experience, I just read about them). Right now my project almost works while using cmake . && make on linux (it works when header and source files are in the same folder, fails when I separate them to include / src folders). I am using nuwen's distribution of mingw on windows (and I know that the toolchain is working, it compiles projects from within Eclipse without any problems).

My project directory looks like this:

engine | |- main | |- include | |- App.h |- CMakeLists.txt (2) |- src | |- main.cc |- App.cc |- CMakeLists.txt (3) |- CMakLists.txt (1)

The contents of the files are very simple (I will remove the include guards, etc for clarity)

App.h:

class App { public: App(); int onExecute(); };

App.cc:

#include <iostream> #include "App.h" App::App() { } int App::onExecute() { std::cout << "inside app.." << '\n'; return 0; }

main.cc:

#include <iostream> #include "App.h" using namespace std; int main(int argc, char* argv[]) { App a; a.onExecute(); std::cout << "inside main.." << '\n'; }

CMakeLists.txt (1) - the main one:

cmake_minimum_required (VERSION 2.6) set (CMAKE_CXX_COMPILER "g++") project (gameengine) add_definitions ( "-Wall -ansi -pedantic") add_subdirectory (${CMAKE_SOURCE_DIR}/main/include) add_subdirectory (${CMAKE_SOURCE_DIR}/main/src) add_executable (engine ${CMAKE_SOURCE_DIR}/main/src/main.cc) target_link_libraries (engine Application)

CMakeLists.txt (2) - inside the include directory

add_library (Application App) set_target_properties (Application PROPERTIES LINKER_LANGUAGE CXX)

CMakeLists.txt (3) - inside the src directory

include_directories (../include)

And this is as far as I got - with some changes (i.e. moving App.cc to the include directory) the whole thing compiles and runs fine on linux - but I can't get the mingw generator to work on Win XP. I hand tuned the CMAKE_MAKE_PROGRAM in the file CMakeCache.txt to point to the proper make.exe (I know that this should be defined as a system variable but as I am working on many different PC's, I don't want to leave junk after me).

My questions are:

1) what are the guidelines for writing multiplatform CMakeLists.txt file (which will work independly of the os and the location of the project files), which preferably will allow me to easy swich my project configuration from one os the other one?

2) how can I address the error of not finding the header file (make gives: (...)\engine\main\src\main.cc:2:17: fatal error: App.h: No such file or directory) ?

Thank you for your time and help.

最满意答案

1)编写多平台CMakeLists.txt文件(它将独立于os和项目文件的位置工作)的指导原则是什么,这最好能让我轻松地从一个操作系统配置另一个操作系统?

好吧,我当然不是专家,但我可以分享我在跨平台基于cmake的项目中的10个月经验。

马上我认为你真的应该使用源代码构建 。 这意味着您不会在代码所在的同一目录中运行cmake; 相反,你创建一个新文件夹,例如engine/build并从那里运行cmake ../main 。 这样你就不会用cmake东西破坏你的源文件,例如CMakeCache.txt等。甚至有一些宏可以用来禁止你的用户进行源内构建。

我还发现创建一组宏文件以帮助为不同平台设置编译器选项很有用。 在工作中,我们有ADD_GCC_FLAG或ADD_MSVC_FLAG等宏,它们检查当前编译器并相应地添加标志。

我认为使用单个.cmake文件将所有项目配置集中在一个地方是一种好习惯。 在工作中,我们所有的CMakeLists.txt以include( ../cmake/configs.cmake ) 。 此文件设置各种选项,例如标准包含目录,默认编译器标志等。

为了缓解包含目录的问题,我建议你在源文件中使用绝对路径而不是相对路径。 定义标准包含目录,例如engine/main/include以及相对于该路径的#include文件。 在您的示例中,如果要包含engine/main/include/somefolder/header.h ,则需要编写#include <somefolder/header.h> (使用<>而不是引号告诉C ++预处理器跳过当前目录在寻找文件时)。


2)如何解决未找到头文件的错误(make give:(...)\ engine \ main \ src \ main.cc:2:17:致命错误:App.h:没有这样的文件或目录)?

您的cmake布局存在许多问题,但是您收到该错误的原因是因为您还需要在CMakeLists.txt (1)调用include_directories 。

除此之外,您的其他CMakeLists.txt文件也存在问题。 在CMakeLists.txt (2) , add_library的参数是错误的; 它应该是../src/App.cc ,否则你只是添加一个空库。 而且你也不需要set_target_properties ,至少在没有正确的add_library参数时也是add_library 。 您还需要在添加库的相同CMakeLists.txt中调用include_directory ; 把它放进去(3)并没有真正做任何事情。

实际上,你不需要在include目录中有一个CMakeLists.txt文件,因为那里没有什么可以构建的。 最好在调用include_directories之后立即将add_library调用放在CMakeLists.txt (3) 。

我希望这能解决你的一些疑虑。

1) what are the guidelines for writing multiplatform CMakeLists.txt file (which will work independly of the os and the location of the project files), which preferably will allow me to easy swich my project configuration from one os the other one?

Well, I'm certainly no expert, but I can share my 10-month experience with a cross-platform cmake-based project.

Right off the bat I think you really should be using out of source builds. This means that you don't run cmake in the same directory where your code is; instead, you create a new folder, e.g. engine/build and run cmake ../main from there. This way you don't clobber your source files with cmake stuff, such as CMakeCache.txt etc. There are even some macros you can use to forbid your users from doing in-source builds.

I also find it useful to create a set of macro files to help set compiler options for different platforms. Here at work we have macros such as ADD_GCC_FLAG or ADD_MSVC_FLAG which check the current compiler and add flags accordingly.

I think it is good practice to have a single .cmake file which concentrates all your project configurations in one place. At work all our CMakeLists.txt start with include( ../cmake/configs.cmake ). This file sets all sorts of options, such as standard include directories, default compiler flags etc.

To assuage your problem with include directories, I suggest you use absolute rather than relative paths in your source files. Define a standard include directory, for instance engine/main/include and always #include files relative to that path. In your example, if you wanted to include engine/main/include/somefolder/header.h, you'd write #include <somefolder/header.h> (using <> instead of quotes tells the C++ preprocessor to skip the current directory when looking for the file).


2) how can I address the error of not finding the header file (make gives: (...)\engine\main\src\main.cc:2:17: fatal error: App.h: No such file or directory) ?

There are a number of issues with your cmake layout, but the reason you were getting that error is because you need to call include_directories in CMakeLists.txt (1) as well.

Besides that, your other CMakeLists.txt files have problems too. In CMakeLists.txt (2), the arguments to add_library are wrong; it should be ../src/App.cc, otherwise you're just adding an empty library. And you don't need that set_target_properties either, at least not if you got the add_library arguments right. You also need a include_directory call in that same CMakeLists.txt that's adding the library; putting it in (3) doesn't really do anything.

Actually, you don't need a CMakeLists.txt file in the include directory, since there's nothing to build there. It's better to put the add_library call in CMakeLists.txt (3), right after calling include_directories.

I hope this clears some of your doubts.

更多推荐

本文发布于:2023-08-07 07:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463148.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时需   项目   平台   cmake   multiplatform

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!