Compiling with g++

本文档简要介绍了如何使用g++编译基本的C++程序,包括使用make、UNIX、GDB和Emacs。g++是GNU C++编译器,通过-I指定头文件目录,-o指定输出文件名,-c生成对象文件,-L指定库目录,-l链接库。同时,文章提到了编译多文件程序的方法和一些常用的编译选项,如-g开启调试,-Wall开启所有警告,-O或-O2开启优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Compiling with g++

reference : https://courses.cs.washington.edu/courses/cse373/99au/unix/g++.html
there are some exciting things in it.

  • using make
  • using UNIX
  • using GDB
  • using Emacs

Executive Summary: This document is a brief description of how to compile basic C++ programs using g++. It provides sample command lines for invoking the g++ compiler and a list of some common compiler options.

What is g++?

g++ is your friendly Gnu C++ compiler. g++ does not handle templates well, but you can use them. This document serves as a very simple bunch of hints to start using g++, and is not meant to be complete. For all the gory details about g++'s options, check out its man page.
Compiling HelloWorld.C
Say you have a file helloworld.C as follows :

#include <stdio.h>

void main (){
    printf("Hello World\n");
}

You can compile and run it from the unix prompt as follows :

% g++ helloworld.C

This creates an executable called “a.out”. You can run it by typing

% ./a.out

Since no executable name was specified to g++, a.out is chosen by default. Use the “-o” option to change the name :

% g++ -o helloworld helloworld.C

creates an executable called “helloworld”.

Include Directories

Sometimes the header files that you write are not in the same directory as the .C file that #include’s it. For example you might have a a file “foo.h” that resides in /homes/me/randomplace/include. If you want to include that file in helloworld.C, you could just give the full path name in the #include, OR you can do the following:
Add
#include <foo.h>
to helloworld.C and compile it with the -I option :

% g++ -o helloworld -I/homes/me/randomplace/include helloworld.C 

This basically tells g++ to look for #include’s in /homes/me/include in addition to other directories you specify with -I

Compiling multiple files

Most likely, you will be compiling separate modules and linking them into a single executable. Here’s the basic idea: compile each .C file into a .o file, then link the .o files (along with any libraries) into an executable. Of course, one of these .C files has to define the main() or else the linker will complain. Suppose we have main.C, foo.C and bar.C and want to create an executable fubar, and suppose further that we need the math library:

% g++ -c -o foo.o foo.C 
% g++ -c -o main.o main.C
% g++ -c -o bar.o bar.C
% g++ -o fubar foo.o main.o bar.o -lm

The first three commands generate foo.o, main.o and bar.o respectively. The last line links them together along with the math library, libm.a.

Some options

-g - turn on debugging (so GDB gives more friendly output)
-Wall - turns on most warnings
-O or -O2 - turn on optimizations
-o - name of the output file
-c - output an object file (.o)
-I - specify an include directory
-L - specify a lib directory
-l - link with library lib.a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值