`
fanrey
  • 浏览: 252208 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

gcc build c++ code

    博客分类:
  • gcc
阅读更多
// testcpp.cpp
#include <iostream>
using namespace std;

int main() {
cout << "Hello World" << endl; // prints Hello World
return 0;
}


g++:
g++ -g testcpp.cpp -o testcpp

gcc:
gcc -g -l stdc++ testcpp.cpp -o testcpp (C++编译,C++链接)


另外, gcc会根据文件名后缀来选择用C还是C++编译器。
//f1.c: gcc 把它当成C语言来编译
extern "C"
{
void f1(){
return;
}
}

//f1.cpp gcc 把它当成C++语言来编译
extern "C"
{
void f1(){
return;
}
}
编译:gcc -c f1.cpp -o f1.o 成功
编译:gcc -c f1.c -o f1.o 失败,报如下错:
f1.c:1:8: error: expected identifier or ‘(’ before string constant
原因是gcc用C编译器来编译f1.c,而C编译器不认识extern "C".
gcc用c++编译器来编译f1.cpp,成功,extern "C"中的语句被当成C语言来编译的。另外cxx后缀也会被认为成C++。

-x language filename
  设定文件所使用的语言,使后缀名无效,对以后的多个有效.也就是根据约定C语言的后
缀名称是.c的,而C++的后缀名是.C或者.cpp,如果你很个性,决定你的C代码文件的后缀
名是.pig 哈哈,那你就要用这个参数,这个参数对他后面的文件名都起作用,除非到了
下一个参数的使用。
  可以使用的参数吗有下面的这些
  `c', `objective-c', `c-header', `c++', `cpp-output', `assembler', and `a
ssembler-with-cpp'.
  看到英文,应该可以理解的。
  例子用法:
  gcc -x c hello.pig


gcc/g++在执行编译工作的时候,总共需要4步
1.预处理,生成.i的文件[预处理器cpp]
2.将预处理后的文件不转换成汇编语言,生成文件.s[编译器egcs]
3.有汇编变为目标代码(机器代码)生成.o的文件[汇编器as]
4.连接目标代码,生成可执行程序[链接器ld]


-c
  只激活预处理,编译,和汇编,也就是他只把程序做成obj文件
  例子用法:
  gcc -c hello.c
  他将生成.o的obj文件
-S
  只激活预处理和编译,就是指把文件编译成为汇编代码。
  例子用法
  gcc -S hello.c
  他将生成.s的汇编代码,你可以用文本编辑器察看
-E
  只激活预处理,这个不生成文件,你需要把它重定向到一个输出文件里面.
  例子用法:
  gcc -E hello.c > pianoapan.txt
  gcc -E hello.c | more

分享到:
评论

相关推荐

    Using GCC with MinGW for VSCode(VSCode 配置开发 C++)

    In this tutorial, you configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger from Mingw-w64 to create programs that run on Windows. As you go through the tutorial, you will ...

    Bloodshed Dev-C++

    * During Dev-C++ First Time COnfiguration window, users can now choose between using or not class browser and code completion features. * Many bug fixes Version 4.9.8.4 * Added the possibility to ...

    Linux中使用VS Code编译调试C++项目详解

    在Linux下C/C++的编译器大多采用gcc/g++。既然要在Linux下进行C++开发,很有必要了解一下g++编译器的一些基本知识。 假设我现在有一个最简单的C++文件: #include using namespace std; int main() { cou

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...

    eclipse 开发c/c++

    在 Window =&gt; Preferences =&gt; C/C++ =&gt; Code Templates 中,可以添加新模板并查看完整的模板列表。 也可以将模板作为 XML 文件导出和导入。 图 5. 预定义的 C/C++ 代码模板 代码历史记录:即使您没有使用 CVS 或...

    w64devkit 1.19.0 免安装C/C++开发组件 for x64 Windows

    w64devkit is a Dockerfile that builds from source a small, portable development suite for creating C and C++ applications on and for x64 Windows. See "Releases" for pre-built, ready-to-use kits. ...

    s2n:TLS / SSL协议的实现-C/C++开发

    git clone https://github.com/${YOUR_GITHUB_ACCOUNT_NAME}/s2n.git cd s2n#从codebuild / codebuild.config文件中选择一个“ env”行并运行它,在这种情况下,选择带有GCC 9版本S2N_LIBCRYPTO = open

    Segger emWin 5.22 Libraries for NXP MCUs

    - arm-none-eabi-gcc gcc version 4.6.2 (LPCXPresso 5.1.0 by Code Red). - IAR ANSI C/C++ Compiler V6.50.1.4415/W32 for ARM Copyright 1999-2012 IAR Systems AB. - ARM C/C++ Compiler, 5.02 [Build 28] [MDK-...

    Android NDK Game Development Cookbook 无水印pdf 0分

    Android NDK is also the key for portability, which in turn provides a reasonably comfortable development and debugging process using familiar tools such as GCC and Clang toolchains. If your wish to ...

    VAX v10.8.2029

    Fixed Change Signature causing loss of code when updating parameters in call reference sites that have binary and ternary operators (regression in build 2007) (case=79955) Change Signature available ...

    深度学习之卷积神经网络CNN模式识别VS代码

    sample code ------ ```cpp #include "tiny_cnn.h" using namespace tiny_cnn; // specify loss-function and optimization-algorithm typedef network, gradient_descent&gt; CNN; // tanh, 32x32 input, 5x5 ...

    VScode配置C++环境,preLaunchTask”g++”已终止,退出代码为1解决办法

    记录一下用VScode配置C++环境时遇到的坑。一开始是按照https://blog.csdn.net/bat67/article/details/76095813来配置的,结果后来出现退出代码为1的错误。后来是根据官方文档解决的。官方文档链接:...

    [修复下载链接]iPhone5S和iPad4上编译C/C++

    make 3.81-2p dependency-based build environments mobile substrate 0.9.5000 powerful code insertion platform mobileterminal-applesdk 520-2 A Terminal emulator for iOS org.coolstar.cctools 836-1-2 ...

    .config:我的

    -recursive https://github.com/yelog/.config.git ~ /.config导入东西新病毒# Download source codegit clone https://github.com/neovim/neovim.git# install cmake and dependencyyum install -y cmake gcc-...

    java笔试题回文子串-training:Java、C++和Python中各种编程挑战的解决方案

    code cpp )并运行构建任务。 在构建后运行测试执行测试任务。 Linux 在 Linux 上需要 gcc-5 和 g++-5 以及 CMake 3.2。 要从控制台构建和运行测试,请转到 cpp 目录( cd ./cpp/ )并运行./build.sh或 mkdir -p ...

    MPC播放器整套源代码,VS2008编译环境

    Media Player Classic 源代码,vs2008编译环境-Media Player Classic source code To compile you need: * TortoiseSVN Download * Microsoft Visual C++ 2008 SP1 Download * Microsoft® Windows® Software ...

    glew-2.1.0.tgz

    c++ opengl的glew, # GLEW - The OpenGL Extension Wrangler Library ![](http://glew.sourceforge.net/glew.png) http://glew.sourceforge.net/ https://github.com/nigels-com/glew [![Build Status]...

    Heilx AAC Decoder optimized for ARM

    section below) fixpt/pub public header files fixpt/real source code for RealNetworks' AAC decoder fixpt/testwrap sample code to build a command-line test application &lt;br&gt;Code organization...

    CentOS7.2.1511 gcc4.8.5 通过编译的 tfs2.2.16

    [root@2f60c4bcddfa /]# yum install make automake autoconf libtool gcc gcc-c++ libuuid-devel zlib-devel mysql-devel readline-devel gperftools-devel.x86_64 -y Libraries have been installed in: /usr/...

    libtomcrypt-1.17

    LibTomCrypt builds out of the box with GCC 2.95 and up as well as Visual C++ v6.00 with SP5. It can be reconfigured to eliminate algorithms, use different build options (e.g. smaller or faster code) ...

Global site tag (gtag.js) - Google Analytics