简体   繁体   English

如何在Linux中编译一个c++程序?

[英]How to compile a c++ program in Linux?

I made a file hi.cpp and I wrote the command given below:我制作了一个文件 hi.cpp 并编写了下面给出的命令:

#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
  return 0;
}

then I ran it in my RHEL 6 machine with the following command然后我使用以下命令在我的 RHEL 6 机器上运行它

gcc hi.cpp

and I got some errors which are as follows:我得到了一些错误,如下所示:

[chankey@localhost ~]$ gcc hi.cpp
/tmp/cc32bnmR.o: In function `main':
hi.cpp:(.text+0xa): undefined reference to `std::cout'
hi.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
hi.cpp:(.text+0x19): undefined reference to `std::cout'
hi.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
/tmp/cc32bnmR.o: In function `__static_initialization_and_destruction_0(int, int)':
hi.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()'
hi.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc32bnmR.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[chankey@localhost ~]$ 

What do these errors denote?这些错误代表什么? My code is correct then why am I getting errors?我的代码是正确的,那为什么我会出错?

Use g++使用 g++

g++ -o hi hi.cpp

g++ is for C++, gcc is for C although with the -libstdc++ you can compile c++ most people don't do this. g++ is for C++, gcc is for C although with the -libstdc++ you can compile c++ most people don't do this.

As the other answers say, use g++ instead of gcc .正如其他答案所说,使用g++而不是gcc

Or use make: make hi或使用 make: make hi

You have to use g++ (as mentioned in other answers).您必须使用 g++ (如其他答案中所述)。 On top of that you can think of providing some good options available at command line (which helps you avoid making ill formed code):最重要的是,您可以考虑在命令行中提供一些不错的选项(这有助于您避免编写格式错误的代码):

g++   -O4    -Wall hi.cpp -o hi.out
     ^^^^^   ^^^^^^
  optimize  related to coding mistakes

For more detail you can refer to man g++ | less更多细节可以参考man g++ | less man g++ | less . man g++ | less

Try this:尝试这个:

g++ -o hi hi.cpp

gcc is only for C gcc 仅适用于 C

$ g++ 1st.cpp -o 1st $ g++ 1st.cpp -o 1st

$./1st $./1st

if you found any error then first install g++ using code as below如果您发现任何错误,请首先使用以下代码安装 g++

$ sudo apt-get install g++ $ sudo apt-get install g++

then install g++ and use above run code然后安装 g++ 并使用上面的运行代码

g++ -o foo foo.cpp

g++ --> Driver for cc1plus compiler g++ --> cc1plus 编译器驱动

-o --> Indicates the output file ( foo is the name of output file here. Can be any name) -o --> 表示output文件(这里的foo是output文件的名字,可以是任意名字)

foo.cpp --> Source file to be compiled foo.cpp --> 要编译的源文件

To execute the compiled file simply type执行编译的文件,只需键入

./foo
  1. To Compile your C++ code use:-要编译您的 C++ 代码,请使用:-

g++ file_name.cpp -o executable_file_name g++ 文件名.cpp -o 可执行文件名

(i) -o option is used to show error in the code (ii) if there is no error in the code_file, then it will generate an executable file. (i) -o 选项用于显示代码中的错误 (ii) 如果 code_file 中没有错误,那么它将生成一个可执行文件。

  1. Now execute the generated executable file:现在执行生成的可执行文件:

./executable_file_name ./executable_file_name

For simple test project, g++ or make standalone are good options as already answered:对于简单的测试项目, g++make Standalone 是很好的选择,因为已经回答:

g++ -o hi hi.cpp

or或者

make hi

For real projects, however, the usage of a project manager is required.但是,对于实际项目,需要使用项目经理。 At the time I write this answer, the most used and open-source is cmake (an alternative could be QT qmake ).在我写这个答案时,最常用和开源的是cmake (替代方案可能是 QT qmake )。

Following is a simple CMake example:以下是一个简单的 CMake 示例:

Make sure you installed cmake on your linux distribution apt-get install cmake or yum install cmake .确保在yum install cmake分发版上apt-get install cmake cmake

Create a file CMakeLists.txt (the name is important) together with your source hi.cpp与您的源hi.cpp一起创建一个文件CMakeLists.txt (名称很重要)

project("hi")

add_executable( hi hi.cpp )

Then compile and run as:然后编译并运行为:

cmake .
make
./hi

This allows the project to scale easily with libraries, sources, and much more.这允许项目使用库、源等轻松扩展 It also makes most IDEs to understand the project properly (Most IDEs accept CMake natively, like kdevelop, qtCreator, etc..)它还使大多数 IDE 能够正确理解项目(大多数 IDE 原生接受 CMake,如 kdevelop、qtCreator 等。)

CMake 与 Qt-Creator 集成的示例

You could also generate Visual-Studio or XCode projects from CMake, in case you decide to port the software to other platforms in the future.您还可以从 CMake 生成 Visual-Studio 或 XCode 项目,以防您决定将来将该软件移植到其他平台。

cmake -G Xcode . #will generate `hi.xcodeproj` you can load on macOS

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

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