如何为 Linux 构建 Visual C++ 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/802970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:21:45  来源:igfitidea点击:

How to build a Visual C++ Project for Linux?

c++visual-studiolinuxvisual-c++

提问by mspoerr

What's the best and easiest way to build (for Linux) a C++ application which was written in Visual Studio? The code itself is ready - I used only cross-platform libs.

构建(适用于 Linux)在 Visual Studio 中编写的 C++ 应用程序的最佳和最简单的方法是什么?代码本身已经准备好了——我只使用了跨平台的库。

Is it possible to prepare everything under Windows in Visual Studio and then build it with a CLI tool under Linux? Are there any docs describing this?

是否可以在 Visual Studio 中在 Windows 下准备所有内容,然后在 Linux 下使用 CLI 工具构建它?有没有描述这个的文档?

EDIT:Some more information:

编辑:更多信息:

  • Libs used: stl, wxwidgets, boost, asio, cryptlib.

  • Very little Linux know-how.

  • 使用的库:stl、wxwidgets、boost、asio、cryptlib。

  • 很少的 Linux 知识。

EDIT#2:I chose the following solution: Make new project with kdevelop and compile everything there.

编辑#2:我选择了以下解决方案:使用 kdevelop 制作新项目并在那里编译所有内容。

采纳答案by OregonGhost

We're using CMake for Linux projects. CMake can generate KDevelop and Visual Studio project files, so you can just create your CMake file as the origin of platform-specific IDE files. The KDevelop generator is fine, so you can edit and compile in KDevelop (which will in turn call Make).

我们在 Linux 项目中使用 CMake。CMake 可以生成 KDevelop 和 Visual Studio 项目文件,因此您可以将 CMake 文件创建为特定于平台的 IDE 文件的来源。KDevelop 生成器很好,因此您可以在 KDevelop 中进行编辑和编译(它将依次调用 Make)。

On the other hand, if you have nothing fancy, you can use CMake or just Make to build the thing on Linux, if you want to stay with your solution file (which is what I'm currently doing for a library with test applications). This approach gets complicated when your build process is more than just "throw these files at the compiler and let's see what it does", so the CMake solution is in my opinion a good thing for cross-platform development.

另一方面,如果你没有什么特别的,你可以使用 CMake 或者只是 Make 在 Linux 上构建这个东西,如果你想保留你的解决方案文件(这是我目前正在为一个带有测试应用程序的库做的事情) . 当您的构建过程不仅仅是“将这些文件扔给编译器,让我们看看它做了什么”时,这种方法会变得复杂,因此在我看来,CMake 解决方案对于跨平台开发来说是一件好事。

回答by pts

Here is how to compile C code manually under Linux without additional build tools:

以下是如何在 Linux手动编译 C 代码而无需额外的构建工具

gcc -s -O2 -o prog -I. -Iincludedir1 -Iincludedir2 -Llibdir1 -Llibdir2 code1.c code2.c -luse1 -luse2

However, if your project is larger than a couple of files, you may want to use a build tool instead(such as CMake, see OregonGhost's answer) to automate compilation of many files and to make it incremental. If you use a build tool, it will run gcc under the hood, with similar arguments above. It's useful to understand the arguments above, so you can troubleshoot if your automated build fails.

但是,如果您的项目比几个文件大,您可能希望改用构建工具(例如 CMake,请参阅 OregonGhost 的答案)来自动编译许多文件并使其增量。如果您使用构建工具,它将在后台运行 gcc,并使用上述类似的参数。了解上述参数很有用,因此您可以在自动构建失败时进行故障排除。

This will compile code1.c and code2.c to executable binary prog, loading .h files from ., includedir1 and includedir2, loading libraries (e.g. libuse1.so* and libuse2.so*) from directories libdir1 and libdir2, using libraries use1 (corresponding to file name libuse1.so*) and use2.

这会将 code1.c 和 code2.c 编译为可执行的二进制 prog,从 .、includedir1 和 containsir2 加载 .h 文件,从目录 libdir1 和 libdir2 加载库(例如 libuse1.so* 和 libuse2.so*),使用库 use1(对应文件名 libuse1.so*) 和 use2。

If you get undefined symbol errors in your .c files, you have to specify more -Iflags. If you get undefined symbol errors in the .o file, you have to specify more -lflags. If you get cannot find -l...errors, you have to specify more -Lflags.

如果在 .c 文件中出现未定义的符号错误,则必须指定更多-I标志。如果在 .o 文件中遇到未定义的符号错误,则必须指定更多-l标志。如果cannot find -l...出现错误,则必须指定更多-L标志。

回答by Rich Turner

8 years later ...

8年后...

I stumbled across this question today and thought you might like to?take a look at Visual C++ for Linux.

我今天偶然发现了这个问题,并认为您可能喜欢?看看Visual C++ for Linux

Released in March 2016, VC++ for Linux allows you to create a?project in VC++, and then build it in a Linux machine/VM?using native Linux tools. What's more, you can also debugyour app from within VS?since the Linux tools allow the VS debugger to drive?GDB in your Linux machine/VM via SSH!!

2016 年 3 月发布的 VC++ for Linux 允许您在 VC++ 中创建一个项目,然后使用原生 Linux 工具在 Linux 机器/VM 中构建它。更重要的是,您还可以从 VS 中调试您的应用程序?因为 Linux 工具允许 VS 调试器通过 SSH 在您的 Linux 机器/VM 中驱动 GDB!

Looks like this is almost exactly what @mspoerr was asking for :)

看起来这几乎正是@mspoerr 所要求的:)

回答by dirkgently

What's the best and easiest way to build a c++ application, which was written with Visual Studio, for Linux? The code itself is ready - I used only cross-plattform libs.

为 Linux 构建使用 Visual Studio 编写的 c++ 应用程序的最佳和最简单的方法是什么?代码本身已准备就绪 - 我只使用了跨平台库。

  • Check if there is anything that is not part of Standard C++
  • Rinse and repeat
  • Create a makefile or a KDevelop project
  • 检查是否有任何不属于标准 C++ 的内容
  • 冲洗并重复
  • 创建 makefile 或 KDevelop 项目

Et voilà!

等等!