安裝c/c++開(kāi)發(fā)環(huán)境
yum install make automake gcc gcc-c++ kernel-devel
編譯
- 過(guò)程
- 預(yù)處理
- 編譯
- 匯編
- 鏈接
創(chuàng)建test_hello目錄,創(chuàng)建main.cpp
mkdir test_hello
vim main.cpp
main.cpp內(nèi)容
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
預(yù)處理
g++ -E main.cpp>main.ii
編譯
g++ -S main.ii
匯編
g++ -c main.s
鏈接
g++ main.o -o main
執(zhí)行程序,打印hello,world!
./main
makefile常見(jiàn)預(yù)定義變量
| 命令格式 | 含義 |
|---|---|
| AR | 庫(kù)文件維護(hù)程序的名稱,默認(rèn)值為ar 創(chuàng)建靜態(tài)庫(kù) |
| AS | 匯編程序的名稱,默認(rèn)值為as |
| CC | C編譯器的名稱,默認(rèn)值為cc |
| CPP | C預(yù)編譯器的名稱,默認(rèn)值為$(CC) –E |
| CXX | C++編譯器的名稱,默認(rèn)值為g++ |
| FC | FORTRAN編譯器的名稱,默認(rèn)值為f77 |
| RM | 文件刪除程序的名稱,默認(rèn)值為rm –f |
| ARFLAGS | 庫(kù)文件維護(hù)程序的選項(xiàng),無(wú)默認(rèn)值 |
| ASFLAGS | 匯編程序的選項(xiàng),無(wú)默認(rèn)值 |
| CFLAGS | C編譯器的選項(xiàng),無(wú)默認(rèn)值 |
| CPPFLAGS | C預(yù)編譯的選項(xiàng),無(wú)默認(rèn)值 |
| CXXFLAGS | C++編譯器的選項(xiàng),無(wú)默認(rèn)值 |
| FFLAGS | FORTRAN編譯器的選項(xiàng),無(wú)默認(rèn)值 |
makefile變量的使用
| 命令格式 | 含義 |
|---|---|
| $* | 不包含擴(kuò)展名的目標(biāo)文件名稱 |
| $+ | 所有的依賴文件,以空格分開(kāi),并以出現(xiàn)的先后的序,可能包含重復(fù)的依賴文件 |
| $< | 第一個(gè)依賴文件的名稱 |
| $? | 所有時(shí)間戳比目標(biāo)文件晚的依賴文件,并以空格分開(kāi) |
| $@ | 目標(biāo)文件的完整名稱 |
| $^ | 所有不重復(fù)的依賴文件,以空格分開(kāi) |
| $% | 如果目標(biāo)是歸檔成員,則該變量表示目標(biāo)的歸檔成員名稱 |
簡(jiǎn)單的makefile使用
創(chuàng)建文件
mkdir first_make
cd first_make
vim first_make.cpp
vim xdata.h
vim xdata.cpp
vim makefile
first_make.cpp
#include <iostream>
#include <thread>
#include "xdata.h"
using namespace std;
void ThreadMain() {
cout << "Thread Main\n" <<endl;
}
int main() {
thread th(ThreadMain);
cout << "first make test" << endl;
th.join();
xdata d;
return 0;
}
xdata.h
#ifndef XDATA_H
#define XDATA_H
class xdata {
public:
xdata();
};
#endif //XDATA_H
xdata.cpp
#include "xdata.h"
#include <iostream>
xdata::xdata() {
std::cout << "Create xdata" << std::endl;
}
makefile(注意Tab鍵縮進(jìn))
first_make: first_make.cpp xdata.cpp
g++ -std=c++11 first_make.cpp xdata.cpp -o first_make -lpthread
編譯&運(yùn)行
make
./first_make
簡(jiǎn)化makefile
first_make: first_make.cpp xdata.cpp
$(CXX) -std=c++11 $^ -o $@ -lpthread
TARGET=first_make
LIBS=-lpthread
$(TARGET): first_make.cpp xdata.cpp
-@rm test
@echo "begin build $(TARGET)"
$(CXX) -std=c++11 $^ -o $@ $(LIBS)
@echo "$(TARGET) build success"
非同級(jí)目錄下文件引入
mkdir include
cd include
vim test_include.h
test_include.h
#ifndef TEST_INCLUDE_H
#define TEST_INCLUDE_H
#define CONF_PATH "/opt/code"
class test_include {
};
#endif //TEST_INCLUDE_H
first_make.cpp引入test_include.h
#include <iostream>
#include <thread>
#include "xdata.h"
#include "test_include.h"
using namespace std;
void ThreadMain() {
cout << "Thread Main\n" <<endl;
}
int main() {
thread th(ThreadMain);
cout << "first make test" << endl;
th.join();
xdata d;
return 0;
}
makefile
TARGET=first_make
LIBS=-lpthread
OBJS=first_make.o xdata.o
CXXFLAGS=-std=c++11 -I./include
$(TARGET): $(OBJS)
@echo "begin build $(TARGET)"
$(CXX) -std=c++11 $^ -o $@ $(LIBS)
@echo "$(TARGET) build success"
makefile clean
TARGET=first_make
LIBS=-lpthread
OBJS=first_make.o xdata.o
CXXFLAGS=-std=c++11 -I./include
$(TARGET): $(OBJS)
@echo "begin build $(TARGET)"
$(CXX) -std=c++11 $^ -o $@ $(LIBS)
@echo "$(TARGET) build success"
clean:
$(RM) $(OBJS) $(TARGET)
.PHONY: clean *clean
makefile clean使用
make
make clean
make