- 編譯程序的命令
g++ hello_world.cpp -o hello
-o定義輸出文件額名字為hello,該輸出文件和源文件在同一個文件夾下面。
運行可執(zhí)行文件hello
./hello
移除文件 rm hello
編譯文件得到可執(zhí)行文件的同時,保留產生的中間文件
g++ -save-temps hello_world.cpp -o hello
單個文件編譯過程:
實際的編譯過程:預處理,對應臨時文件hello_world.ii
g++ -E hello_world.cpp -o preprocessed.ii
cat preprocessed.ii
預處理的工作主要包含去掉注釋,然后將我們include的庫tack上,以上過程使我們自己主動調用預處理器的過程
cat hello_world.ii
則展示了我們在第5)中編譯時保留的中間文件,兩者是一樣的
實際的編譯過程:這是真正的編譯過程compilation step,對應臨時文件hello_world.s(assembly code)
我們自己把.ii文件進行assembly code得到.s的方法。
g++ -S preprocessed.ii -o complied.s
.s文件是高級語言和機器語言之間的中間環(huán)節(jié)
實際的編譯過程:Assembly,將.s對應的assembly code轉換成.o對應的機器語言的過程,也叫machine-readable code或者object code
讓編譯器將.s文件assembly起來
g++ -c complied.s -o assembled.o
實際的編譯過程:最后一步Linking,產生最終的可執(zhí)行文件。
"Undefined reference" errors are pretty much always linking errors, and you will probably have them. Remember this.
我們通過連接一堆.o文件,得到.exe文件的過程,命令:
g++ assembled.o -o hello_manual
多個文件編譯過程:
舉例,如果我們有定義一個class,然后我們的文件中包含dog.hpp,dog.cpp和main.cpp三個文件,然后我們只使用以下兩個命令:
g++ -c main.cpp -o main.o
g++ main.o dog_program
的話就會出錯,告訴我們undefined reference of dog::bark()
因為對于不同的.cpp文件,都要生成一個object file,也就是.o文件。所以如果我們用以下命令:
g++ -c main.cpp -o main.o
g++ -c dog.cpp
g++ dog.o main.o -o dog_program
的話,就不會出錯。
我們如果修改main.cpp中的內容的話,我們只需要重新用最后一個連接命令。但是,如果我們修改了dog class本身的內容的話,例如添加一個函數,我們就需要重新產生object file of dog.cpp,然后重新連接。
關于Make的介紹
用自己常用的記事本創(chuàng)建一個Makefile,并注意大小寫。在program對應的目錄下面。
gedit Makefile
Makefile里面語句的書寫規(guī)則是
Target: tgt_dependency1 tgt_dependency2 ……
Command
所以dog.o和main.o對應的語句分別是:
dog.o: dog.hpp dog.cpp
g++ -c dog.cpp
main.o: main.cpp
g++ -c main.cpp
在Makefile中Tab是很重要的,所以不要忘記在command對應的第二行加Tab
Makefile的編譯順序
如果Makefile中有如下語句
animal_assembly : moose goose cat
command
moose : antlers hooves fur
command
goose : beak wings webbed_feet interest_in_bread
command
cat : whiskers evil_personality
command
我們可以看到animal_assembly的dependency是 moose goose cat。如果文件夾中存在moose goose cat的話,make命令只需要執(zhí)行第一句就可以了。如果文件夾中缺少moose goose cat中的一個或者幾個,make命令執(zhí)行的時候,需要先找到moose goose cat的生成方法,然后執(zhí)行對應的命令,最后執(zhí)行animal_assembly生成的命令。
moose : antlers hooves fur
command
animal_assembly : moose goose cat
command
goose : beak wings webbed_feet interest_in_bread
command
cat : whiskers evil_personality
command
如果Makefille是以上形式的話,我們只運行make的話,Makefile會只執(zhí)行第一句,因為第一句的dependency都存在了,所以只把moose生成的命令執(zhí)行完就好了。如果我們要生成animal_assembly,就要運行命令make animal_assembly。所以,我們需要把最重要的命令,我們最重要生成的object file對應的命令放在第一行。
所以我們的dog_program的完整的Makefile文件應該是:
dog_program: dog.o main.o
g++ dog.o main.o -o dog_program
dog.o: dog.hpp dog.cpp
g++ -c dog.cpp
main.o: main.cpp
g++ -c main.cpp
在Makefile的最后寫clean語句。
clean:
rm dog_program *.o
然后我們在命令窗口運行make clean的話,就會刪除文件夾中生成的可執(zhí)行文件,和所有過程中產生的副產品。
對于Makefile中的dependency list,我們需要將每個object file的dependency list都寫好。因為make不負責檢查文件中的具體的語句,只負責執(zhí)行Makefile中的語句。
dog_program:
g++ dog.o main.o -o dog_program
dog.o: dog.hpp dog.cpp
g++ -c dog.cpp
main.o: main.cpp
g++ -c main.cpp
如果像上面所示去掉dog_program的dependency list的話,運行make就會出錯,因為main是依賴于dog.cpp的。
如果文件中本身就存在dog.o和main.o的話,運行make不會出錯,因為make就是先check dog.o main.o是否存在,存在的話就直接運行。
所以,我們如果修改了main.cpp或者dog.cpp的話,我們需要重新生成dog.o和main.o。因為make是不管這個問題的。
make這個命令的功能就是執(zhí)行Makefile中我們要求執(zhí)行的語句,對結果沒有任何的預期,也不會檢查命令有沒有問題。所以,我們必須遵守Makefile書寫中的一些規(guī)則。
all : fill_file_with_nonsense
echo "I have mostly created a lot of junk today!"
fill_file_with_nonsense : create_file
echo "Hello, there is nothing important here" > silly_file
create_file :
touch silly_file touch是Unix中的典型命令,用于生成空的文件
move_file :
mv silly_file silly_file_new_name
delete_file :
rm _file
open_file :
gedit another_silly_file
clean :
touch junk1 junk2 junk3 junk4 junk5
really_clean :
rm junk*
如果想體驗的更加清楚,就可以運行這個文件中的內容,然后就知道m(xù)ake完全不會管結果是什么,只是沒有腦子的執(zhí)行命令。
解釋上面的內容:
Makefile的書寫規(guī)則。all: 放在第一句,把所以我們要生成executable依賴的Targets列出來,這樣我們需要的所有的文件都可以生成。我們看到all對應的dependency是file_file_with_nonsense,就去找file_file_with_nonsense的生成語句,發(fā)現它的dependency是create_file,然后去找create_file的生成語句,就到touch silly_file,touch是Unix中的典型命令,用于生成空的文件。create_file的語句執(zhí)行完之后,回到file_file_with_nonsense,執(zhí)行echo "Hello, there is nothing important here" > silly_file,就把"Hello, there is nothing important here" 寫入silly_file中,file_file_with_nonsense的語句也執(zhí)行完之后,我們就返回到all,然后在命令行輸出"I have mostly created a lot of junk today!"。
因為其他的target,不在all的dependency list中,也不在all的dependency的dependency當中,所以只能通過make target的形式來調用對應的命令。
Marvelous macros(宏)
一個宏的示例,宏就是Makefile中的variables,用來定義我們需要的操作,一些變量之類的
CXX = clang++
FLAGS = -O
hello : hello_world.cpp
$(CXX) $(FLAGS) $? -o $@
clean :
rm hello
CXX,這是一個預定義的宏,default value是g++。這里把CXX定義成clang++了。
FLAGS,這里定義的是-O。FLAGS也不一定非得定義成-o,也可以是some moose have large antlers,但是這樣定義的話,就會導致調用的時候出錯。
對于上面的文件,我們在命令行輸入make的時候,實際運行的是clang++ -O hello_world.cpp -o hello。
如果我們把CXX=clang++這一行刪掉的話,在命令行輸入make,實際運行的就是g++ -O hello_world.cpp -o hello。
定義好macro宏,我們使用的時候,就要用$(MACRO)這樣的形式,這是makefile語言的一種語法。我們注意到MACRO全部用的大寫,雖然不是明確規(guī)定的,但是通常情況下用大寫。
$?和$@是makefile language里面特別的預定義的宏。$?是指的"names of the dependencies(newer than the target)",$@是指的"name of the target"。
Complier and liner flags in CS 225
This defines our compiler and linker, as we've seen before.
CXX = clang++ LD = clang++
These are the options we pass to the compiler.
-std=c++1y means we want to use the C++14 standard (called 1y in this version of Clang).
-stdlib=libc++ specifies that we want to use the standard library implementation called libc++
-c specifies making an object file, as you saw before
-g specifies that we want to include "debugging symbols" which allows us to use a debugging program.
-O0 specifies to do no optimizations on our code.
-Wall, -Wextra, and -pedantic tells the compiler to look out for common problems with our code. -Werror makes it so that these warnings stop compilation.
CXXFLAGS = -std=c++1y -stdlib=libc++ -c -g -O0 -Wall -Wextra -Werror -pedantic
These are the options we pass to the linker.
The first two are the same as the compiler flags.
-l<something> tells the linker to go look in the system for pre-installed object files to link with.
Here we want to link with the object files from libpng (since we use it in our code) and libc++. Remember libc++ is the standard library implementation.
LDFLAGS = -std=c++1y -stdlib=libc++ -lpng -lc++abi