简体   繁体   English

在Windows与Linux中运行C ++程序

[英]Running a C++ program in Windows vs Linux

I've been told by my teacher that my homework (a simple program, I chose to code in C++) must be runnable in Linux. 我的老师告诉我,我的家庭作业(一个简单的程序,我选择用C ++编写代码)必须在Linux中运行。 Here's what he said exactly: 这就是他所说的:

"...you should also include a README file that contains all command lines that should run by cut-and-paste. If you used C, then “...你还应该包含一个README文件,其中包含应该通过剪切和粘贴运行的所有命令行。如果你使用了C,那么

 $ gcc –o 2010-11649-hairpin 2010-11649-hairpin.c
 $ 2010-11649-hairpin –input filename –l 200 –m 4 –h 20

"

Where -l 200 , -m 4 and the like are parameters of the program. 其中-l 200-m 4等是程序的参数。

I've always coded in Windows, and I have very little experience running programs from the command line, so I'm not sure what I need to do to make sure my program can run in Linux. 我总是在Windows中编码,而且我从命令行运行程序的经验很少,所以我不确定我需要做些什么来确保我的程序可以在Linux中运行。 Can someone help me figure out what I need to do? 有人能帮我弄清楚我需要做什么吗? I've written the program, I just need to make sure everything works when the TA tries to run it in Linux. 我已经编写了程序,我只需要确保TA尝试在Linux中运行它时一切正常。

First of all you will have to try this by executing your instructions on Linux. 首先,您必须通过在Linux上执行指令来尝试此操作。

If you have written your program in standard C/C++ it will compile and then run on Linux just fine. 如果你用标准的C / C ++编写程序,它将编译然后在Linux上运行就好了。 Once you get to the less basic parts, you have big chances you have used OS specific headers and it might not work as easily. 一旦你找到不太基本的部分,你很有可能使用了特定于操作系统的标题,它可能不会那么容易。

When compiling a C++ program under Linux, normally you want to use g++ the C++ compiler in gcc . 在Linux下编译C ++程序时,通常你想在gcc使用g++ C ++编译器。 Depending on your program, replacing gcc with g++ in your example might work (although your filenames probably are different). 根据您的程序,在您的示例中用g++替换gcc可能会起作用(尽管您的文件名可能不同)。 So you might end up with something like: 所以你最终会得到类似的东西:

g++ –o myprogram myprogram.cpp
./myprogram –l 200 –m 4

The above will only work for basic programs with all source code in a single directory. 以上内容仅适用于所有源代码都在一个目录中的基本程序。

One fast way to see that your program compiles in gcc is to use an online compiler like ideone . 查看程序在gcc中编译的一种快速方法是使用像ideone这样的在线编译器。 Paste you code there and select C++ 4.9.whateverversion ( gcc ) and then compile and run. 将代码粘贴到那里并选择C ++ 4.9.whateverversion( gcc )然后编译并运行。

The alternative is to install a linux OS, easiest and most convenient way in a virtual machine. 另一种方法是在虚拟机中安装最简单,最方便的Linux操作系统。 You can use the free oracle virtualbox or VMWare . 您可以使用免费的oracle virtualboxVMWare

Setting up linux in a vm is easy 在vm中设置linux很容易

There are other answers here showing you how to compile your program from command line in linux. 这里还有其他答案,向您展示如何从linux中的命令行编译程序。

If your C++ program uses also just one file (eg hairpin.cpp), you could use almost the exact same commands, except that you replace gcc by g++ and hairpin.c by hairpin.cpp . 如果您的C ++程序也只使用一个文件(例如hairpin.cpp),那么您可以使用几乎完全相同的命令,除了用g++替换gcchairpin.chairpin.cpp

However, if you've got enough time and system resources, I would recommend you try compiling and running everything in a virtual machine. 但是,如果您有足够的时间和系统资源,我建议您尝试在虚拟机中编译和运行所有内容。 VirtualBox is a good and free virtualization software, and Linux Mint is in my opinion a very beginner-friendly distribution. VirtualBox是一款优秀且免费的虚拟化软件,而Linux Mint在我看来是一个非常适合初学者的版本。

Testing everything yourself on a VM is probably the best solution to ensure everything works as expected. 在VM上自行测试所有内容可能是确保一切按预期工作的最佳解决方案。

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

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