简体   繁体   English

将 Visual Studio C++ 项目迁移到 Linux 和 CMake

[英]Migrating a Visual Studio C++ Project to Linux and CMake

I'm currently trying to move from Windows 10 to Linux (Pop,_OS).我目前正在尝试从 Windows 10 移动到 Linux (Pop,_OS)。 but I'm having trouble getting my C++ Project to compile and run correctly on the latter, My C++ project was created using Visual Studio, where I also specified the include folders, library folders, what should be linked.但我无法让我的 C++ 项目在后者上正确编译和运行,我的 C++ 项目是使用 Visual Studio 创建的,我还指定了包含文件夹、库文件夹以及应该链接的内容。 etc in the solution properties.等在解决方案的属性。 I now want to switch to writing my code using Neovim and not Visual Studio (or Visual Studio Code) and have tried compiling it via G++, I quickly noticed that my include files weren't recognized.我现在想改用 Neovim 而不是 Visual Studio(或 Visual Studio Code)编写我的代码,并尝试通过 G++ 编译它,我很快注意到我的包含文件无法识别。 so I tried to use CMake and created a CMakeLists.txt.所以我尝试使用 CMake 并创建了一个 CMakeLists.txt。 I tried using both我尝试同时使用

INCLUDE_DIRECTORIES()

and

TARGET_INCLUDE_DIRECTORIES()

but no matter what path I enter, my included files were not recognized.但无论我输入什么路径,都无法识别我包含的文件。 Even when I used a path to the specific include file that caused the first error, it still wasn't recognized.即使当我使用导致第一个错误的特定包含文件的路径时,它仍然无法识别。

My goal would be that I can specify an include folder and a library folder, so that I can just add files and folders in these and that the new files and folders automatically get recognized when compiling (ie I would not have to edit the CMakeLists.txt in the future).我的目标是我可以指定一个包含文件夹和一个库文件夹,这样我就可以在其中添加文件和文件夹,并且在编译时自动识别新文件和文件夹(即我不必编辑 CMakeLists. txt 在未来)。 Is that even possible with CMake and if yes, does anyone know where i can find further information about that or does anyone have a CMakeLists.txt file that does this? CMake 甚至可能吗?如果是,有人知道我在哪里可以找到有关它的更多信息,或者有人有执行此操作的 CMakeLists.txt 文件吗? If no, would I have to specify each and every file and folder in the CMakeLists.txt file and do the same for every new include and library?如果不是,我是否必须指定 CMakeLists.txt 文件中的每个文件和文件夹,并对每个新的包含和库执行相同的操作?

Project structure:项目结构:

Overall folder
\- build
\- include
---> includeFolder1
---> includeFolder2
---> ...
\- libs
---> library1.lib
---> library2.lib
---> ...
\- src
--> main.cpp
--> other .cpp's and .h's
--> other folders with .cpp's and .h's

I've tried compiling with G++ and CMake, but both did not work, no matter what I specified as the include and library paths.我试过使用 G++ 和 CMake 进行编译,但无论我指定什么作为包含和库路径,两者都不起作用。

I have found the problem that caused my errors.我找到了导致我出错的问题。 The problem wasn't with CMake, it was with Windows and Linux specific details.问题不在于 CMake,而在于 Windows 和 Linux 的具体细节。 I always received errors like "<foo\foo.h> no such file or directory", which led me to think that CMake couldn't find the include directory or the files in it.我总是收到类似“<foo\foo.h> 没有这样的文件或目录”的错误,这让我认为 CMake 找不到包含目录或其中的文件。 The problem, however, is with the include path itself.然而,问题在于包含路径本身。 On Windows, paths can be given with a backslash ('\') but on Linux, paths are denominated with a forward slash ('/').在 Windows 上,路径可以用反斜杠 ('\') 给出,但在 Linux 上,路径用正斜杠 ('/') 命名。 So in my example, the path to the file was "../foo/foo.h" but my code had "#include <foo\foo.h>".所以在我的例子中,文件的路径是“../foo/foo.h”,但我的代码有“#include <foo\foo.h>”。 So when migrating a project from Windows to Linux, be sure to watch out for backslashes in your #include statements!因此,在将项目从 Windows 迁移到 Linux 时,一定要注意 #include 语句中的反斜杠!

Below is a template CMakeLists.txt, that should be a good starting point if you want to migrate your Visual Studio project to Linux. I've used glfw (+ glad) as an example library:下面是模板 CMakeLists.txt,如果您想将 Visual Studio 项目迁移到 Linux,这应该是一个很好的起点。我使用 glfw (+ glad) 作为示例库:

cmake_minimum_required(VERSION 3.20)

project(ExampleProject)

add_executable(${PROJECT_NAME} src/glad.c src/main.cpp)

target_include_directories(${PROJECT_NAME} PRIVATE include)

target_link_libraries(${PROJECT_NAME} GL dl glfw)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用CMake生成Visual Studio C ++项目文件 - Using CMake to generate Visual Studio C++ project files 列出由cmake生成的Visual Studio C ++项目中的头文件 - Listing header files in Visual Studio C++ project generated by cmake Visual Studio构建托管在GitHub中的C ++ / CMake项目 - Visual Studio to build C++/CMake project hosted in GitHub 将Visual C ++项目迁移到Visual Studio 2013-DirectShow基类错误C2169 - Migrating Visual C++ project to Visual Studio 2013 - DirectShow baseclasses error C2169 最简单的方法是“移植”Visual Studio C ++项目以在Linux上编译? - Easiest way to “port” a Visual Studio C++ project to compile on Linux? 如何在Linux上有效地编译Visual Studio C ++项目 - How to compile a visual studio c++ project on linux efficiently 错误:将Visual C ++项目迁移到Visual Studio 2013之后,MFC需要使用Winsock2.h - Error : MFC requires use of Winsock2.h after migrating Visual C++ project to Visual Studio 2013 将MFC中的大项目从Visual C ++ 6.0迁移到Visual Studio 2005 - Migrating a big project in MFC from Visual C++ 6.0 to Visual Studio 2005 Visual Studio C++ 项目为Linux,如何运行.out文件? - Visual Studio C++ Project for Linux, how to run .out file? Visual Studio C ++和Linux - Visual Studio C++ and Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM