GCC Compile Flow

General Flow

Preprocess: g++ -E test.cpp -o test.i
Compile: g++ -S test.i
Assemble: g++ -c test.s
Link: g++ test.o
SourceFile: test.cpp
PreprocessedFile: test.i
AssembleFile: test.s
ObjectiveFile: test.o
ExecutableFile: a.out

Build static library and link it

Build static library

Preprocess: g++ -E test.cpp -o test.i
Compile: g++ -S test.i
Assemble: g++ -c test.s
AR: ar -r libtest.a test.o
SourceFile: test.cpp
PreprocessedFile: test.i
AssembleFile: test.s
ObjectiveFile: test.o
Static Library: libtest.a

Link static library

Preprocess: g++ -E main.cpp -o main.i
Compile: g++ -S main.i
Assemble: g++ -c main.s
g++ main.o libtest.a
SourceFile: main.cpp
PreprocessedFile: main.i
AssembleFile: main.s
ObjectiveFile: main.o
libtest.a
a.out

OR

g++ main.cpp -L./ -ltest
libtest.a
a.out
SourceFile: main.cpp

OR

g++ main.cpp libtest.a
test.a
a.out
SourceFile: main.cpp

Build dynamic library and link

Build dynamic library

g++ -E test.cpp -o test.i
g++ -S test.i
g++ -c test.s
g++ -shared test.o -o libtest.so
test.cpp
test.i
test.s
test.o
libtest.so

Build executable file with dynamic link

Preprocess: g++ -E main.cpp -o main.i
Compile: g++ -S main.i
Assemble: g++ -c main.s
g++ main.o libtest.so
SourceFile: main.cpp
PreprocessedFile: main.i
AssembleFile: main.s
ObjectiveFile: main.o
libtest.a
a.out

OR

g++ main.cpp -L./ -ltest
libtest.so
a.out
main.cpp

OR

g++ main.cpp libtest.so
libtest.so
a.out
main.cpp

Attached source files

#define RETURN_IF(expr, code) { if(expr) {return code;}}

int isEven(int num);
#include "test.h"
#include <iostream>

using namespace std;

int isEven(int num)
{
    if (num % 2 == 0)
    {
        cout << num << " is Even" << endl;
    } 
    else
    {
        cout << num << " is not Even" << endl;
    }
    return 0;
}
#include "test.h"
#include <iostream>

using namespace std;

int main()
{
    return isEven(99);
}

Attached common commands options

command optionmean
-Estop after preprocessor, generate .i files.
-SStop after compile and generate .s files.
-cStop after assemble and generate .o files.
-oadd output file after option.
-sharedGenerate dynamic library.
-LAdd search path for libraries. For example -L./ means add current path to library search path.
-lAdd link needed library. For example -ltest means add libtest.a or libtest.so to link.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值