gdb_action gcc_action g++ Visual Studio windows下编译Boost库

 

C++ Boost库:windows下编译Boost库

在源代码目录下  

打开cmd,运行

C:\cpplib\boost_1_84_0>bootstrap.bat

C:\cpplib\boost_1_84_0>b2  在当前目录的stage中生成lib目录

libboost_filesystem-vc140

C:\cpplib\boost_1_84_0>b2   --toolset=msvc-14.3

libboost_filesystem-vc143

 

 

 

方式一

点【编辑】-【高级】-【设置选定内容的格式】

方式二

选中需要调整格式的内容(可用CTRL+A),然后再按Ctrl + K 和Ctrl + F 就好了

快捷键调整

常用快捷键自定义 窗口中进行查看
1.进入【工具】-【选项】 对话框

2.选择【环境】-【键盘】

3.在 【显示命令包含】 下面的对话框中输入“对齐”关键字,然后就会在这个编辑框下面一个文本窗口中显示关于“对齐”一系列的“某某对齐”操作命令,选中你想查看的,就会在【选定命令的快捷键】一栏中显示其快捷键了。

常用:
注释代码 Ctrl+K+C(comment)
反注释代码 Ctrl+K+U

键盘映射方案 快捷键方案

 

 

 

 

 

在linux中执行

yum install gdb-gdbserver

 

Package gdb-gdbserver-7.6.1-120.el7.x86_64 already installed and latest version

 调试模式gdbserver

 

 

运行mqtt库

https://eclipse.dev/paho/files/mqttdoc/MQTTClient/html/subasync.html

实践

 

 

 

 前置:

GitHub - eclipse/paho.mqtt.c: An Eclipse Paho C client library for MQTT for Windows, Linux and MacOS. API documentation: https://eclipse.github.io/paho.mqtt.c/

下载paho.mqtt.c

cd paho.mqtt.c

$ cmake --build .

$ cmake --build . --target install

$ cmake --build . --target package

 【注意连接器的附加依赖项paho-mqtt3c.lib在cmake生成的Eclipse-Paho-MQTT-C-1.3.13-win64.zip解压后的lib】

(没有这一项,则报错:链接器工具错误 LNK2019)

 

windows安装cmake

 

 

必须要加入动态链接库选项,否则编译可能会出现以下错误:

MQTT C Client Library - 知乎 https://zhuanlan.zhihu.com/p/54450815

 

 

【实践  编译后事件复制ddl文件】

 

xcopy /y /d configGITignore.ini "$(OutDir)"
xcopy /y /d "..\bizDdl\*dll" "$(OutDir)"
cd "$(OutDir)"
ren configGITignore.ini config.ini

 

 

安装boost

 

【安装boost】
1)C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\bin\Hostx64\x64
添加path

cl.exe

bootstrap.bat

2)
PS C:\cpplib\boost_1_65_1> .\bootstrap.bat
Building Boost.Build engine
cl: 命令行 warning D9035 :“GZ”选项已否决,并将在将来的版本中移除
cl: 命令行 warning D9036 :使用“RTC1”而不使用“GZ”
cl: 命令行 warning D9002 :忽略未知选项“/MLd”

Failed to build Boost.Build engine.
Please consult bootstrap.log for further diagnostics.

You can try to obtain a prebuilt binary from

http://sf.net/project/showfiles.php?group_id=7586&package_id=72941

Also, you can file an issue at http://svn.boost.org
Please attach bootstrap.log in that case.

bootstrap.log


###
### Using 'msvc' toolset.
###

C:\cpplib\boost_1_65_1\tools\build\src\engine>if exist bootstrap rd /S /Q bootstrap

C:\cpplib\boost_1_65_1\tools\build\src\engine>md bootstrap

C:\cpplib\boost_1_65_1\tools\build\src\engine>cl /nologo /GZ /Zi /MLd /Fobootstrap/ /Fdbootstrap/ -DNT -DYYDEBUG kernel32.lib advapi32.lib user32.lib /Febootstrap\jam0 command.c compile.c constants.c debug.c execcmd.c execnt.c filent.c frames.c function.c glob.c hash.c hdrmacro.c headers.c jam.c jambase.c jamgram.c lists.c make.c make1.c object.c option.c output.c parse.c pathnt.c pathsys.c regexp.c rules.c scan.c search.c subst.c timestamp.c variable.c modules.c strings.c filesys.c builtins.c md5.c class.c cwd.c w32_getreg.c native.c modules/set.c modules/path.c modules/regex.c modules/property-set.c modules/sequence.c modules/order.c
command.c
C:\cpplib\boost_1_65_1\tools\build\src\engine\jam.h(71): fatal error C1034: ctype.h: 不包括路径集
compile.c
C:\cpplib\boost_1_65_1\tools\build\src\engine\jam.h(71): fatal error C1034: ctype.h: 不包括路径集
constants.c
debug.c
C:\cpplib\boost_1_65_1\tools\build\src\engine\jam.h(71): fatal error C1034: ctype.h: 不包括路径集
execcmd.c
C:\cpplib\boost_1_65_1\tools\build\src\engine\jam.h(71): fatal error C1034: ctype.h: 不包括路径集
execnt.c
C:\cpplib\boost_1_65_1\tools\build\src\engine\jam.h(71): fatal error C1034: ctype.h: 不包括路径集

解决办法:
添加Windows环境变量
INCLUDE
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt
LIB
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64


成功
C:\cpplib\boost_1_65_1>bootstrap.bat
Building Boost.Build engine
cl: 命令行 warning D9035 :“GZ”选项已否决,并将在将来的版本中移除
cl: 命令行 warning D9036 :使用“RTC1”而不使用“GZ”
cl: 命令行 warning D9002 :忽略未知选项“/MLd”

Bootstrapping is done. To build, run:

.\b2

To adjust configuration, edit 'project-config.jam'.
Further information:

- Command line help:
.\b2 --help

- Getting started guide:
http://boost.org/more/getting_started/windows.html

- Boost.Build documentation:
http://www.boost.org/build/doc/html/index.html

C:\cpplib\boost_1_65_1>
Win10 + VS2019 编译安装 Boost - 知乎 https://zhuanlan.zhihu.com/p/160347338


 执行结束后,会在当前目录下生成 b2.exe 文件。然后双击执行该可执行文件,并等待其执行结束。

VS2019配置C++ boost库 - 牛犁heart - 博客园 https://www.cnblogs.com/whiteBear/p/14017131.html

 


在 stage\lib 生成 执行完生成的库

【VS2022】
项目——>属性——>选择VC++目录,在包含目录 中添加
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LibraryPath>$(LibraryPath)</LibraryPath>
<IncludePath>C:\cpplib\boost_1_65_1;$(IncludePath)</IncludePath>
</PropertyGroup>

#include <boost/lexical_cast.hpp>
#include <iostream>

using namespace std;
using namespace boost;

int main()
{
//system("chcp 65001");

double a = lexical_cast<double>("3.1415926");
string str = lexical_cast<string>("3.1415926");
cout << "This is a number: " << a << endl;
cout << "This is a string: " << str << endl;
int b = 0;
try {
b = lexical_cast<int>("neo");
}
catch (bad_lexical_cast& e) {
cout << e.what() << endl;
}
return 0;
}

 

 

 

 

 

 

C++调试 .vscode目录json文件作用
Tasks in Visual Studio Code https://code.visualstudio.com/docs/editor/tasks

c_cpp_properties.json reference https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference
Customize default settings in Visual Studio Code C++ projects https://code.visualstudio.com/docs/cpp/customize-default-settings-cpp
Get Started with C++ and MinGW-w64 in Visual Studio Code https://code.visualstudio.com/docs/cpp/config-mingw

 

c_cpp_properties.json 【vscode代码编辑窗口寻找库路径】

{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\cpplib\\boost_1_65_1"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22621.0",
"compilerPath": "C:/msys64/ucrt64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}

tasks.json 【vscode调试不同语言的通用的调试配置,输出命令行信息】


launch.json 【对tasks.json编译的文件 gdb调试】

settings.json [未知]

 

MinGW-w64 https://www.mingw-w64.org/

Mingw-w64 is an advancement of the original mingw.org project, created to support the GCC compiler on Windows systems. It has forked it in 2007 in order to provide support for 64 bits and new APIs. It has since then gained widespread use and distribution.

The development and community are very active and welcoming with new contributors every month and simple installers.

 

Download MinGW-w64 - for 32 and 64 bit Windows https://sourceforge.net/projects/mingw-w64/files/latest/download

 

MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software.

It consists of a command line terminal called mintty, bash, version control systems like git and subversion, tools like tar and awk and even build systems like autotools, all based on a modified version of Cygwin. Despite some of these central parts being based on Cygwin, the main focus of MSYS2 is to provide a build environment for native Windows software and the Cygwin-using parts are kept at a minimum. MSYS2 provides up-to-date native builds for GCC, mingw-w64, CPython, CMake, Meson, OpenSSL, FFmpeg, Rust, Ruby, just to name a few.

To provide easy installation of packages and a way to keep them updated it features a package management system called Pacman, which should be familiar to Arch Linux users. It brings many powerful features such as dependency resolution and simple complete system upgrades, as well as straight-forward and reproducible package building. Our package repository contains more than 2900 pre-built packages ready to install.

For more details see 'What is MSYS2?' which also compares MSYS2 to other software distributions and development environments like CygwinWSLChocolateyScoop, ... and 'Who Is Using MSYS2?' to see which projects are using MSYS2 and what for.

 

 

msys2是一个在windows上使用gnu库的环境。它包含多个软件包库(package repositories):

  • msys
  • mingw64
  • mingw32
  • clang64
  • clang32
  • ucrt64

所以我们安装好后能看到多个可执行程序:msys2.exe、mingw64.exe、clang64.exe等。

其中msys2.exe是主入口,在这里可以安装任意库的软件包。

 

2.2 安装msys2

可以在官网下载:https://www.msys2.org/

也可以在清华镜像下载:

https://mirrors.tuna.tsinghua.edu.cn/msys2/distrib/x86_64/?C=M&O=D

在里面找最新的exe安装包即可。

 

安装好后按照https://mirrors.tuna.tsinghua.edu.cn/help/msys2/ 中的指导设置镜像源,这样下载包会比较快。

 

2.3 安装编译工具链

msys2中可用的工具链有以下几种:

https://packages.msys2.org/basegroups/mingw-w64-toolchain

 

若要安装gcc则执行

pacman -S mingw-w64-x86_64-toolchain

 

若要安装clang则执行

pacman -S mingw-w64-clang-x86_64-toolchain

 

 

 

 打开 msys2 

 

 

vscode C c++环境

C++ programming with Visual Studio Code https://code.visualstudio.com/docs/languages/cpp

 

gcc -E hello.c -o hello.i
cpp  hello.c > hello.i
 

 gcc -S a.c;cat a.s 查看汇编代码。

 

gdb a.out

start 

n

 

install gdb

https://sourceware.org/gdb/download/

 sudo apt-get install libc6-dbg gdb valgrind

 

[test@ecs autocloudservices]# yum install python-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package python-devel-2.7.5-80.el7_6.x86_64 already installed and latest version
Nothing to do
[test@ecs autocloudservices]# which yum
/usr/bin/yum


[test@ecs autocloudservices]# yum search python3 | grep devel
boost-python36-static.x86_64 : The Python3 Boost C++ static development
python36-cairo-devel.x86_64 : Libraries and headers for python36-cairo
python36-greenlet-devel.x86_64 : C development headers for python36-greenlet
python36-qt5-devel.x86_64 : Development files for python3-qt5
boost-python36-devel.x86_64 : Shared object symbolic links for Boost.Python 3
boost169-mpich-python3-devel.x86_64 : Shared library symbolic links for
boost169-openmpi-python3-devel.x86_64 : Shared library symbolic links for
boost169-python3-devel.x86_64 : Shared object symbolic links for Boost.Python 3
python34-devel.x86_64 : Libraries and header files needed for Python 3
: development
python36-devel.x86_64 : Libraries and header files needed for Python development
python36-gobject-devel.x86_64 : Development files for embedding Python 3.6
python36-idle.x86_64 : A basic graphical development environment for Python
python36-pillow-devel.x86_64 : Development files for pillow
python36-sip-devel.x86_64 : Files needed to generate Python bindings for any C++
shiboken-python36-devel.x86_64 : Development files for shiboken
[test@ecs autocloudservices]#

注意这里是关键yum install python36-devel.x86_64

 

 

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++

 

 windows gcc安装

 

1、 MSYS2 https://www.msys2.org/

2、Now MSYS2 is ready for you and a terminal for the UCRT64 environment will launch. 执行命令 $ pacman -S mingw-w64-ucrt-x86_64-gcc

3、添加路径 C:\msys64\ucrt64\bin

 

-2-

pacman -S mingw-w64-ucrt-x86_64-gcc

 

In this terminal, install the MinGW-w64 toolchain by running the following command:

pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
Accept the default number of packages in the toolchain group by pressing Enter.

C++ programming with Visual Studio Code https://code.visualstudio.com/docs/languages/cpp

 

 

 https://code.visualstudio.com/docs/cpp/launch-json-reference

 

 

 

 

 


resolving dependencies...
looking for conflicting packages...

Packages (15) mingw-w64-ucrt-x86_64-binutils-2.41-2
mingw-w64-ucrt-x86_64-crt-git-11.0.0.r239.g037ba0184-1
mingw-w64-ucrt-x86_64-gcc-libs-13.2.0-2 mingw-w64-ucrt-x86_64-gmp-6.3.0-2
mingw-w64-ucrt-x86_64-headers-git-11.0.0.r239.g037ba0184-1
mingw-w64-ucrt-x86_64-isl-0.26-1 mingw-w64-ucrt-x86_64-libiconv-1.17-3
mingw-w64-ucrt-x86_64-libwinpthread-git-11.0.0.r239.g037ba0184-1
mingw-w64-ucrt-x86_64-mpc-1.3.1-2 mingw-w64-ucrt-x86_64-mpfr-4.2.1-2
mingw-w64-ucrt-x86_64-windows-default-manifest-6.4-4
mingw-w64-ucrt-x86_64-winpthreads-git-11.0.0.r239.g037ba0184-1
mingw-w64-ucrt-x86_64-zlib-1.3-1 mingw-w64-ucrt-x86_64-zstd-1.5.5-1
mingw-w64-ucrt-x86_64-gcc-13.2.0-2

Total Download Size: 49.38 MiB
Total Installed Size: 418.89 MiB

:: Proceed with installation? [Y/n] y
:: Retrieving packages...
mingw-w64-ucrt-x86_64-head... 6.0 MiB 2.02 MiB/s 00:03 [###############################] 100%
mingw-w64-ucrt-x86_64-gcc-... 966.9 KiB 1243 KiB/s 00:01 [###############################] 100%
mingw-w64-ucrt-x86_64-isl-... 1452.1 KiB 351 KiB/s 00:04 [###############################] 100%
mingw-w64-ucrt-x86_64-zstd... 621.5 KiB 1372 KiB/s 00:00 [###############################] 100%
mingw-w64-ucrt-x86_64-gmp-... 578.5 KiB 707 KiB/s 00:01 [###############################] 100%
mingw-w64-ucrt-x86_64-mpfr... 535.7 KiB 1191 KiB/s 00:00 [###############################] 100%
mingw-w64-ucrt-x86_64-libi... 719.9 KiB 326 KiB/s 00:02 [###############################] 100%
mingw-w64-ucrt-x86_64-mpc-... 128.3 KiB 294 KiB/s 00:00 [###############################] 100%
mingw-w64-ucrt-x86_64-winp... 40.7 KiB 107 KiB/s 00:00 [###############################] 100%
mingw-w64-ucrt-x86_64-zlib... 92.0 KiB 115 KiB/s 00:01 [###############################] 100%
mingw-w64-ucrt-x86_64-binu... 6.1 MiB 857 KiB/s 00:07 [###############################] 100%
mingw-w64-ucrt-x86_64-libw... 30.2 KiB 55.0 KiB/s 00:01 [###############################] 100%
mingw-w64-ucrt-x86_64-crt-... 3.4 MiB 455 KiB/s 00:08 [###############################] 100%
mingw-w64-ucrt-x86_64-wind... 3.0 KiB 7.04 KiB/s 00:00 [###############################] 100%
mingw-w64-ucrt-x86_64-gcc-... 28.8 MiB 2.49 MiB/s 00:12 [###############################] 100%
Total (15/15) 49.4 MiB 4.24 MiB/s 00:12 [###############################] 100%
(15/15) checking keys in keyring [###############################] 100%
(15/15) checking package integrity [###############################] 100%
(15/15) loading package files [###############################] 100%
(15/15) checking for file conflicts [###############################] 100%
(15/15) checking available disk space [###############################] 100%
:: Processing package changes...
( 1/15) installing mingw-w64-ucrt-x86_64-libwinpthread-git [###############################] 100%
( 2/15) installing mingw-w64-ucrt-x86_64-gcc-libs [###############################] 100%
( 3/15) installing mingw-w64-ucrt-x86_64-zstd [###############################] 100%
( 4/15) installing mingw-w64-ucrt-x86_64-binutils [###############################] 100%
( 5/15) installing mingw-w64-ucrt-x86_64-headers-git [###############################] 100%
( 6/15) installing mingw-w64-ucrt-x86_64-crt-git [###############################] 100%
( 7/15) installing mingw-w64-ucrt-x86_64-gmp [###############################] 100%
( 8/15) installing mingw-w64-ucrt-x86_64-isl [###############################] 100%
( 9/15) installing mingw-w64-ucrt-x86_64-libiconv [###############################] 100%
(10/15) installing mingw-w64-ucrt-x86_64-mpfr [###############################] 100%
(11/15) installing mingw-w64-ucrt-x86_64-mpc [###############################] 100%
(12/15) installing mingw-w64-ucrt-x86_64-windows-default... [###############################] 100%
(13/15) installing mingw-w64-ucrt-x86_64-winpthreads-git [###############################] 100%
(14/15) installing mingw-w64-ucrt-x86_64-zlib [###############################] 100%
(15/15) installing mingw-w64-ucrt-x86_64-gcc [###############################] 100%

 

 

 

 

MinGW | Minimalist GNU for Windows http://www.mingw.org/

 

MinGW, a contraction of "Minimalist GNU for Windows", is a minimalist development environment for native Microsoft Windows applications.

MinGW provides a complete Open Source programming tool set which is suitable for the development of native MS-Windows applications, and which do not depend on any 3rd-party C-Runtime DLLs. (It does depend on a number of DLLs provided by Microsoft themselves, as components of the operating system; most notable among these is MSVCRT.DLL, the Microsoft C runtime library. Additionally, threaded applications must ship with a freely distributable thread support DLL, provided as part of MinGW itself).

MinGW compilers provide access to the functionality of the Microsoft C runtime and some language-specific runtimes. MinGW, being Minimalist, does not, and never will, attempt to provide a POSIX runtime environment for POSIX application deployment on MS-Windows. If you want POSIX application deployment on this platform, please consider Cygwin instead.

Primarily intended for use by developers working on the native MS-Windows platform, but also available for cross-hosted use, (see note below -- you may need to follow the "read more" link to see it), MinGW includes:

  • A port of the GNU Compiler Collection (GCC), including C, C++, ADA and Fortran compilers;
  • GNU Binutils for Windows (assembler, linker, archive manager)
  • A command-line installer, with optional GUI front-end, (mingw-get) for MinGW and MSYS deployment on MS-Windows
  • A GUI first-time setup tool (mingw-get-setup), to get you up and running with mingw-get.

MSYS, a contraction of "Minimal SYStem", is a Bourne Shell command line interpreter system. Offered as an alternative to Microsoft's cmd.exe, this provides a general purpose command line environment, which is particularly suited to use with MinGW, for porting of many Open Source applications to the MS-Windows platform; a light-weight fork of Cygwin-1.3, it includes a small selection of Unix tools, chosen to facilitate that objective.

 




 

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 invokedg++ 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

gcc和g++的区别
 
  我们在编译c/c++代码的时候,有人用gcc,有人用g++,于是各种说法都来了,譬如c代码用gcc,而c++代码用g++,或者说编译用gcc,链接用g++,一时也不知哪个说法正确,如果再遇上个extern "C",分歧就更多了,这里我想作个了结,毕竟知识的目的是令人更清醒,而不是更糊涂。
 
误区一:gcc只能编译c代码,g++只能编译c++代码
两者都可以,但是请注意:
1.后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的,例如:
#include <stdio.h>
int main(int argc, char* argv[])
{
   if(argv == 0) return;
   printString(argv);
   return;
}
int printString(char* string)
{
  sprintf(string, "This is a test.\n");
}
如果按照C的语法规则,OK,没问题,但是,一旦把后缀改为cpp,立刻报三个错:
“printString未定义”;
“cannot convert `char**' to `char*”;
”return-statement with no value“;
    这3个错分别对应前面红色标注的部分。可见C++的语法规则更加严谨一些。
 
2.编译阶段,g++会调用gcc,对于c++代码,两者是等价的,但是因为gcc命令不能自动和C++程序使用的库联接,所以通常用g++来完成链接,为了统一起见,干脆编译/链接统统用g++了,这就给人一种错觉,好像cpp程序只能用g++似的。
 
误区二:gcc不会定义__cplusplus宏,而g++会
实际上,这个宏只是标志着编译器将会把代码按C还是C++语法来解释,如上所述,如果后缀为.c,并且采用gcc编译器,则该宏就是未定义的,否则,就是已定义。
 
误区三:编译只能用gcc,链接只能用g++
严格来说,这句话不算错误,但是它混淆了概念,应该这样说:编译可以用gcc/g++,而链接可以用g++或者gcc -lstdc++。因为gcc命令不能自动和C++程序使用的库联接,所以通常使用g++来完成联接。但在编译阶段,g++会自动调用gcc,二者等价。
 
误区四:extern "C"与gcc/g++有关系
实际上并无关系,无论是gcc还是g++,用extern "c"时,都是以C的命名方式来为symbol命名,否则,都以c++方式命名。试验如下:
me.h
extern "C" void CppPrintf(void);
 
me.cpp:
#include <iostream>
#include "me.h"
using namespace std;
void CppPrintf(void)
{
     cout << "Hello\n";
}
 
test.cpp:
#include <stdlib.h>
#include <stdio.h>
#include "me.h"        
int main(void)
{
    CppPrintf();
    return 0;
}
 
1. 先给me.h加上extern "C",看用gcc和g++命名有什么不同
[root@root G++]# g++ -S me.cpp     //g++的参数-S: 是指把文件编译成为汇编代码
[root@root G++]# less me.s
.globl _Z9CppPrintfv        //注意此函数的命名
        .type   CppPrintf, @function
[root@root GCC]# gcc -S me.cpp
[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++]# g++ -S me.cpp
[root@root G++]# less me.s
.globl _Z9CppPrintfv        //注意此函数的命名
        .type   _Z9CppPrintfv, @function
完全相同!
【结论】完全相同,可见extern "C"与采用gcc/g++并无关系,以上的试验还间接的印证了前面的说法:在编译阶段,g++是调用gcc的。
转自:http://lpy999.blog.163.com/blog/static/117372061201182042232511/
 
 
 
 

 

 

 

posted @   papering  阅读(235)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示