link options and how g++ is invoked gcc g++
yum install gcc
yum install gcc-c++
yum reinstall gcc gcc-c++
Downloading packages:
(1/2): gcc-c++-4.8.5-28.el7_5.1.x86_64.rpm | 7.2 MB 00:00:00
(2/2): gcc-4.8.5-28.el7_5.1.x86_64.rpm | 16 MB 00:00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------
Total 47 MB/s | 23 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : gcc-4.8.5-28.el7_5.1.x86_64 1/2
Installing : gcc-c++-4.8.5-28.el7_5.1.x86_64 2/2
Verifying : gcc-4.8.5-28.el7_5.1.x86_64 1/2
Verifying : gcc-c++-4.8.5-28.el7_5.1.x86_64 2/2
Installed:
gcc.x86_64 0:4.8.5-28.el7_5.1 gcc-c++.x86_64 0:4.8.5-28.el7_5.1
Complete!
[root@txy ~]# which gcc
/usr/bin/gcc
[root@txy ~]# which g++
/usr/bin/g++
https://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html
Next: C Dialect Options, Previous: Overall Options, Up: Invoking GCC [Contents][Index]
3.3 Compiling C++ Programs
C++ source files conventionally use one of the suffixes ‘.C’, ‘.cc’, ‘.cpp’, ‘.CPP’, ‘.c++’, ‘.cp’, or ‘.cxx’; C++ header files often use ‘.hh’, ‘.hpp’, ‘.H’, or (for shared template code) ‘.tcc’; and preprocessed C++ files use the suffix ‘.ii’. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc
).
However, the use of gcc
does not add the C++ library. g++
is a program that calls GCC and automatically specifies linking against the C++ library. It treats ‘.c’, ‘.h’ and ‘.i’ files as C++ source files instead of C source files unless -x is used. This program is also useful when precompiling a C header file with a ‘.h’ extension for use in C++ compilations. On many systems, g++
is also installed with the name c++
.
When you compile C++ programs, you may specify many of the same command-line options that you use for compiling programs in any language; or command-line options meaningful for C and related languages; or options that are meaningful only for C++ programs. See Options Controlling C Dialect, for explanations of options for languages related to
Using the GNU Compiler Collection (GCC): Link Options https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
c++ - What is the difference between g++ and gcc? - Stack Overflow https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc/172592#172592
gcc
and g++
are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler).
Even though they automatically determine which backends (cc1
cc1plus
...) to call depending on the file-type, unless overridden with -x language
, they have some differences.
The probably most important difference in their defaults is which libraries they link against automatically.
According to GCC's online documentation link options and how g++ is invoked, g++
is equivalent to gcc -xc++ -lstdc++ -shared-libgcc
(the 1st is a compiler option, the 2nd two are linker options). This can be checked by running both with the -v
option (it displays the backend toolchain commands being run).
gcc和g++的区别 - 苦涩的茶 - 博客园 https://www.cnblogs.com/liushui-sky/p/7729838.html
两者都可以,但是请注意:
if(argv == 0) return;
}
int printString(char* string)
sprintf(string, "This is a test.\n");
}
实际上,这个宏只是标志着编译器将会把代码按C还是C++语法来解释,如上所述,如果后缀为.c,并且采用gcc编译器,则该宏就是未定义的,否则,就是已定义。
严格来说,这句话不算错误,但是它混淆了概念,应该这样说:编译可以用gcc/g++,而链接可以用g++或者gcc -lstdc++。因为gcc命令不能自动和C++程序使用的库联接,所以通常使用g++来完成联接。但在编译阶段,g++会自动调用gcc,二者等价。
实际上并无关系,无论是gcc还是g++,用extern "c"时,都是以C的命名方式来为symbol命名,否则,都以c++方式命名。试验如下:
me.h:
extern "C" void CppPrintf(void);
#include <iostream>
#include "me.h"
using namespace std;
void CppPrintf(void)
{
cout << "Hello\n";
}
#include <stdlib.h>
#include <stdio.h>
#include "me.h"
int main(void)
{
CppPrintf();
return 0;
}
[root@root G++]# g++ -S me.cpp //g++的参数-S: 是指把文件编译成为汇编代码
[root@root G++]# less me.s
.globl _Z9CppPrintfv //注意此函数的命名
.type CppPrintf, @function
[root@root GCC]# less me.s
.globl _Z9CppPrintfv //注意此函数的命名
.type CppPrintf, @function
完全相同!
2. 去掉me.h中extern "C",看用gcc和g++命名有什么不同
[root@root GCC]# gcc -S me.cpp
[root@root GCC]# less me.s
.globl _Z9CppPrintfv //注意此函数的命名
.type _Z9CppPrintfv, @function
[root@root G++]# less me.s
.globl _Z9CppPrintfv //注意此函数的命名
.type _Z9CppPrintfv, @function
完全相同!
最新文章
- 对比MS Test与NUnit Test框架
- Redis哈希-hash
- html之colspan &;&; rowspan讲解
- ios状态栏调整 简单动画的知识点
- 正则化方法 exec 和match以及test
- winfrom 多语言切换
- U口破解指令介绍.
- PHP SOCKET 编程
- (转)log4j(五)——如何控制不同目的地的日志输出?
- ubuntu下创建python的虚拟环境
- python问题:AttributeError: &#39;module&#39; object has no attribute &#39;SSL_ST_INIT&#39;
- Query the tables and index which will caus rebuild index fail
- mysql01
- 手把手在MyEclipse中搭建Hibernate开发环境
- ldconfig命令与ldd命令
- django之创建第10个项目-图片上传方式1
- aircrack-ng无线破解实验
- NIO流—理解Buffer、Channel概念和NIO的读写操作
- 修改索引名称(mysql)
- 正排索引(forward index)与倒排索引(inverted index) (转)
热门文章
- spine findBone
- android 建数据库 SQLite 存储sd 卡或者内存
- 音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系
- Sublime Text的列模式
- 问题-Delphi7中JSON遍历节点不支持使用IN处理方法?
- CCRenderTexture画点出现十字架";歪解";
- C#调用C++dll文件 运行提示找不到函数的入口点
- 多线程中wait和notify的理解与使用
- java打印和重写toString
- Windows下 Zmq v4.0.4的使用 (C++)