【问题标题】:Creating a ".o" file from a Visual Studio 2012 project从 Visual Studio 2012 项目创建“.o”文件
【发布时间】:2013-07-15 15:56:17
【问题描述】:

我在 Visual Studio 2012 中编写了一个类程序,我想在 Linux 机器上运行它。我试过在 Linux 机器上编译,但我相信我使用的一些库不是跨平台兼容的。无论如何我可以创建一个可以在 Linux 上运行的 .o 文件,而不需要我更改我的代码?

语言:C++ 以下是我使用的库:

#include <iostream>
#include <queue>
#include <map>
#include <climits> 
#include <iterator>
#include <algorithm>
#include <fstream>
#include <string>
#include <iomanip>

当我尝试在 Linux 中编译时会发生什么

Main.cpp: In function ‘int main()’:
Main.cpp:110:24: error: no matching function for call to             'std::basic_ifstream<char>::basic_ifstream(std::string&)’
Main.cpp:110:24: note: candidates are:
/usr/include/c++/4.6/fstream:460:7: note: std::basic_ifstream<_CharT,     _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char, _Traits     = std::char_traits<char>, std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.6/fstream:460:7: note:   no known conversion for argument 1 from     ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
/usr/include/c++/4.6/fstream:446:7: note: std::basic_ifstream<_CharT,     _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/fstream:446:7: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/4.6/fstream:420:11: note:     std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)
/usr/include/c++/4.6/fstream:420:11: note:   no known conversion for argument 1 from     ‘std::string {aka std::basic_string<char>}’ to ‘const std::basic_ifstream<char>&’

【问题讨论】:

  • 在linux上编译会发生什么?
  • 这看起来像是 C++11 的扩展(打开一个带有 string 作为参数的文件)。尝试使用-std=c++11 [或-std=c++0x,如果您有旧版本的 gcc]。

标签: c++ linux visual-studio-2012 compilation g++


【解决方案1】:

您列出的所有头文件似乎都不特定于 Visual Studio 或 Windows,因此假设您在 Linux 机器上安装了正确的 C++ 标准库,它应该可以编译。

如果您真的想要交叉编译,那么您必须将gcc 的源代码以及 C++ 标准库头文件和二进制文件下载到您的 Windows 机器上。然后为交叉编译构建编译器(这可能意味着安装“windows to Unix compatible version of gcc”,例如 Cygnus cygwin 或 gcc MingW)。实现这一目标并不是一蹴而就的。相信我,除非你真的必须这样做,否则你不想这样做。

如果您出于某种原因不想在 linux 机器本身上编译,一个更好的解决方案是在您的 Windows 机器上安装一个虚拟机,然后在其上安装 Linux,然后使用该 Linux VM 作为您的编译器系统。

编辑:

为了澄清,根据问题中的编辑:问题似乎是 C++11 功能的使用,这可能是 Visual Studio 中的默认设置,其中 gcc/g++ 将采取更保留的方法。解决方案是告诉编译器在构建的命令行上使用带有-std=c++11 的C++ 11 标准。这应该允许使用字符串中的名称打开文件的扩展正常工作。

【讨论】:

    【解决方案2】:

    老实说,你最好用对跨平台更友好的东西来构建它,尤其是看到它是 C++,没有理由依赖 Visual Studio。尝试查看类似 code::blocks 的内容来构建它以在 linux 上运行。

    【讨论】:

      【解决方案3】:

      我认为即使你找到了方法,那也行不通。需要在 Linux Machine 中单独生成 .o

      【讨论】:

        猜你喜欢
        • 2012-08-28
        • 1970-01-01
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        相关资源
        最近更新 更多