简体   繁体   English

从Visual Studio 2012项目创建“ .o”文件

[英]Creating a “.o” file from a Visual Studio 2012 project

I wrote a program for class in Visual Studio 2012, and I want to run it on a Linux machine. 我在Visual Studio 2012中为类编写了一个程序,并且希望在Linux机器上运行它。 I have tried compiling on the Linux machine, but i believe some of the libraries I used are not cross platform compatible. 我曾尝试在Linux机器上进行编译,但是我相信我使用的某些库与跨平台不兼容。 Is there anyway I can create an .o file that will run on Linux, that won't require me to change my code? 无论如何,我可以创建一个可以在Linux上运行的.o文件,不需要我更改代码吗?

Language: C++ Here are the libraries I used: 语言:C ++这是我使用的库:

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

What happens when I try to compile in Linux 当我尝试在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>&’

None of the header files that you list appear to be specific to Visual Studio or Windows, so assuming you have installed the correct C++ standard library on the Linux machine, it should just compile. 您列出的头文件似乎都没有特定于Visual Studio或Windows,因此假设您在Linux机器上安装了正确的C ++标准库,则应该对其进行编译。

If you really want to cross-compile, then you will have to download the source code for gcc , and the C++ standard library headers and binaries onto your Windows machine. 如果您真的想交叉编译,则必须将gcc的源代码以及C ++标准库头文件和二进制文件下载到Windows机器上。 Then build the compiler for cross-compilation (which probably means installing a "windows to Unix compatible version of gcc", such as Cygnus cygwin or gcc MingW). 然后构建用于交叉编译的编译器(这可能意味着安装“ Windows到Unix兼容版本的gcc的窗口”,例如Cygnus cygwin或gcc MingW)。 And it's not going to be straight forward to achieve this. 而且要实现这一目标将不是直截了当的。 Trust me, you don't want to do this unless you REALLY have to. 相信我,除非您真的需要,否则您不想这样做。

A better solution, if you don't want to compile on the linux machine itself for some reason, is to get a virtual machine installed on your Windows machine, and install Linux on that, then use that Linux VM as your compile system. 如果由于某种原因不想在linux机器上进行编译,更好的解决方案是在Windows机器上安装一个虚拟机,然后在其上安装Linux,然后将该Linux VM用作编译系统。

Edit: 编辑:

To clarify, based on the edit in the question: The problem appears to be usage of C++11 features, which is presumably default in Visual Studio, where the gcc/g++ will take a more reserved approach. 需要澄清的是,基于问题中的编辑:问题似乎是C ++ 11功能的使用,这大概是Visual Studio中的默认设置,其中gcc / g ++将采用更为保留的方法。 The solution is to tell the compiler to use the C++ 11 standard with -std=c++11 on the command line for the build. 解决方案是告诉编译器在构建的命令行上将C ++ 11标准与-std=c++11一起使用。 This should allow the extension of opening a file with the name in a string to work correctly. 这应该允许扩展使用字符串中的名称打开文件才能正常工作。

In all honesty, you would be better off building it in something that's a little more cross platform friendly, and especially seeing it's C++, there's no reason to rely on visual studio. 老实说,您最好以更加跨平台友好的方式来构建它,尤其是看到它是C ++时,没有理由依赖Visual Studio。 Try looking at something like code::blocks to build it to run on linux. 尝试查看类似于code :: blocks的东西,以构建可在Linux上运行的代码。

I think even if you find the way, that will not going to work. 我认为,即使您找到方法,也不会奏效。 You need to generate .o separately in Linux Machine 您需要在Linux机器中单独生成.o

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM