c++ 在客户端的GCC使用
c++ 在客户端的GCC使用
翻译自:
GCC是GNU工具箱的一个功能,GNU还包括如下功能:
- GNU compiler collection (GCC)
- GNU make
- GNU Debugger (GDB)
- ....
GCC目前在所有的linux系统都支持
本文在osx系统实现,linux类似, windows需要(MinGW or Cygwin GCC)
查看gcc版本
gcc --version
gcc --help
编译c语言
gcc hello.c
设置输出文件名为hello
gcc -o hello hello.c
编译c++
g++ -o hello hello.cpp
g++ -wall -g -o Hello Hello.cpp
-o 说明输出文件名
-Wall 输出所有warning
-g: 生成附带的debug 信息为gdb debugger用
分离compile和link
g++ -c -Wall -g Hello.cpp
g++ -g -o Hello Hello.o
-c compile产生.o文件, -g利用Hello.o输出应用
compile并link多个源文件
g++ -o myprog.exe file1.cpp file2.cpp
更常见的方式
g++ -c file1.cpp
g++ -c file2.cpp
g++ -o myprog.exe file1.o file2.o
compile into a shared Library
为了把C/C++文件编译到共享库中(win中是.dll,unix是.so),使用-shared选项
GCC编译流程
预处理
cpp hello.c > hello.i
编译The compiler compiles the pre-processed source code into assembly code for a specific processor.
gcc -S hello.i
The -S option specifies to produce assembly code, instead of object code.
The resultant assembly file is "hello.s".
Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file "hello.o"
as -o hello.o hello.s
Linker: Finally, the linker (ld.exe) links the object code with the library code to produce an executable file "hello.exe".
ld -o hello.exe hello.o ...libraries...
Headers (.h), Static Libraries (.lib, .a) and Shared Library (.dll, .so)
- A static library has file extension of ".a" (archive file) in Unixes or ".lib" (library) in Windows。the machine code of external functions used in your program is copied into the executable.
- A shared library has file extension of ".so" (shared objects) in Unixes or ".dll" (dynamic link library) in Windows. When your program is linked against a shared library, only a small table is created in the executable.most operating systems allows one copy of a shared library in memory to be used by all running programs, thus, saving memory
Searching for Header Files and Libraries (-I, -L and -l)
When compiling the program, the compiler needs the header files to compile the source codes; the linker needs the libraries to resolve external references from other object files or libraries. The compiler and linker will not find the headers/libraries unless you set the appropriate options
For each of the headers used in your source (via #include directives), the compiler searches the so-called include-paths for these headers. The include-paths are specified via -Idir option (or environment variable CPATH).
Try list the default include-paths in your system used by the "GNU C Preprocessor" via
cpp -v
Try running the compilation in verbose mode (-v) to study the library-paths (-L) and libraries (-l) used in your system:
gcc -v -o hello.exe hello.c
GCC Environment Variables
1.PATH: For searching the executables and run-time shared libraries (.dll, .so).
2.CPATH: For searching the include-paths for headers. It is searched after paths specified in -I options. C_INCLUDE_PATH and CPLUS_INCLUDE_PATH can be used to specify C and C++ headers if the particular language was indicated in pre-processing.
3.LIBRARY_PATH: For searching library-paths for link libraries. It is searched after paths specified in -L options.
make
Running make without argument starts the target "all" in the makefile. A makefile consists of a set of rules. A rule consists of 3 parts: a target, a list of pre-requisites and a command, as follows:
target: pre-req-1 pre-req-2 ...
command
Use "tab" to indent the command (NOT spaces).
Comment & Continuation
A comment begins with a # and lasts till the end of the line. Long line can be broken and continued in several lines via a back-slash ().
target1 [target2 ...]: [pre-req-1 pre-req-2 ...][command1
command2
......]
Phony Targets
A target that does not represent a file is called a phony target. For example, the "clean" in the above example, which is just a label for a command. The standard phony targets are: all, clean, install
Variables
variable begins with a $ and is enclosed within parentheses (...) or braces {...}.
Automatic Variables
Automatic variables are set by make after a rule is matched.
$@: the target filename.
$*: the target filename without the file extension.
$<: the first prerequisite filename....
Virtual Path - VPATH & vpath
You can use VPATH (uppercase) to specify the directory to search for dependencies and target files. For example,
Search for dependencies and targets from "src" and "include" directories
The directories are separated by space
VPATH = src include
You can also use vpath (lowercase) to be more precise about the file type and its search directory. For example,
vpath %.c src
vpath %.h include
Pattern Rules
c++ 在客户端的GCC使用的更多相关文章
- [000]socket通信--server和client实现的简单例子
在C语言中就学过scanf和printf,其实理解socket就跟这两个输入输出差不多,只不过是信息的传输而已. 1.TCP服务器端(server)的默认函数调用顺序: 按照上述的调用顺序,我们可以来 ...
- rpc简介、原理、实例
简介 RPC(Remote Procedure Call,远程过程调用)是建立在Socket之上的,出于一种类比的愿望,在一台机器上运行的主程序,可以调用另一台机器上准备好的子程序,就像LPC(本地过 ...
- unix网络编程-配置unp.h头文件
第一步进入:www.unpbook.com,下载unp的随书代码.新建一个目录,将压缩包拷贝到这一目录下面,然后将压缩包直接解压:tar -zxvf 压缩包名.tar.gz 完成上一步后,进入到un ...
- Socket服务端和客户端(C++,CodeBlocks+GCC编译)
//main.cpp 1 #include "j_socket.h" #include <stdio.h> #include <pthread.h> ; j ...
- Linux 平台静默安装 Oracle客户端
需求:Linux平台,安装完整版Oracle客户端 Tips:如果只是用到sqlldr,sqlplus功能,可以参考<Linux上oracle精简版客户端快速部署>快速部署精简版:如果需要 ...
- ssl客户端与服务端通信的demo
服务端程序流程 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <str ...
- CentOS7.2 编译安装SVN1.9.5客户端
背景 原来想在Linux机上开Samba共享,在Windows机上把工作目录映射到网络驱动器,用Source Insight编辑代码后就不用来回同步文件了. 然而在使用中发现,Windows机用的SV ...
- Centos 6.5 SNMP客户端安装及配置版本net-snmp-5.7.3
Centos 6.5 SNMP客户端安装及配置SNMP版本:net-snmp-5.7.3.tar.gz1.下载软件cd /usr/local/srcyum -y install gccwget htt ...
- 在Linux下如何使用GCC编译程序、简单生成 静态库及动态库
最近在编写的一个Apache kafka 的C/C++客户端,,在看他写的 example中,他的编译是用librdkafka++.a和librdkafka.a 静态库编译的,,,而我们这 ...
随机推荐
- 前端之JavaScript第一天学习(1)-JavaScript 简介
javaScript 是世界上最流行的编程语言. 这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等设备. JavaScript 是脚本语言 JavaSc ...
- 对git认识
Github则是一个基于Git的日益流行的开源项目托管库.它的使用流程不需要联机,可以先将对代码的修改,评论,保存在本机.等上网之后,再实时推送过去.同时它创建分支与合并分支更容易,推送速度也更快,配 ...
- HDU 5647 DZY Loves Connecting 树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5647 题解: 令dp[u][0]表示u所在的子树中所有的包含i的集合数,设u的儿子为vi,则易知dp ...
- POSIX 线程详解 一种支持内存共享的简捷工具
线程是有趣的 了解如何正确运用线程是每一个优秀程序员必备的素质.线程类似于进程.如同进程,线程由内核按时间分片进行管理.在单处理器系统中,内核使用时间分片来模拟线程的并发执行,这种方式和进程的相同.而 ...
- 【BZOJ】【1640】【USACO2007 Nov】/【1692】【USACO2007 Dec】队列变换
后缀数组/贪心 每次从等待序列的头或尾拿出一个放到答案序列的末尾,那么每次贪心比较头和尾的字典序大小即可…… TAT贪心很好想,但是我一开始没想到是可以直接比较字符串大小……而是一位一位判的,WA了… ...
- 事务并发处理: DB+ORM+逻辑代码
在学习了马士兵有关事务并发处理的视频后, 感觉对事务并发处理的概念,问题以及解决方式有了一定的了解,赶紧记录下来以备后用. 1. 事务:一系列操作要么都完成,要么一个都不完成 2. 事务并发:多个事务 ...
- yield curve
1. A yield curve can be built using deposit rates, swap rates, and future/forward rates 2. A par-cou ...
- CrowdFlower Winner's Interview: 1st place, Chenglong Chen
CrowdFlower Winner's Interview: 1st place, Chenglong Chen The Crowdflower Search Results Relevance c ...
- [百度空间] [转]关于Direct3D多窗口编程的一篇翻译
Introduction In DirectX 8, support for rendering to multiple windows is provided through the creatio ...
- self._raiseerror(v) File "D:\GameDevelopment\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror
D:\BaiDuYun\Plist>python unpack_plist.py lobbyRelieveTraceback (most recent call last): File &quo ...