跳到主要內容

發表文章

目前顯示的是有「gcc」標籤的文章

GCC參數的意義

Debug -g讓產生的.o檔會保留給gdb的資訊,也因此產出物會較大。 -g -Wall –ansi Release -O將程式最佳化,提升運行效率。 -O -Wall –ansi Common Lib Path 如果在程式中引用的標頭檔如下,你就可以不用管-I參數, #include "include/utils.h" 但如果如下,就必須要設定Lib搜尋路徑。 #include <utils.h> libPath=include -I$(libPath)

C++ Programming on Eclipse

Compile Problem could not read symbols 在剛建立完shared或static lib的專案第一次編譯時,都會出現如下方的錯誤: Building target: libSharedLibParc.so Invoking: GCC C++ Linker g++ -shared -o"libSharedLibParc.so" ./DiskInfo.o /usr/bin/ld: ./DiskInfo.o: relocation R_X86_64_32S against `vtable for DiskInfo' can not be used when making a shared object; recompile with -fPIC ./DiskInfo.o: could not read symbols: Bad value collect2: ld returned 1 exit status make: *** [libSharedLibParc.so] Error 1 其實參考它給與的錯誤訊息,將-fPIC加在GCC C++/C Compiler的Miscellaneous中就可以編過了。 -fPIC(position independent code)用途可以參考 link 。

How to build a executable file with static link can run on 32bit and 64bit Linux OS?

做一個跨平台的軟體,一定會遇到如何讓程式可以在32與64位元上執行。也許你會說:我的32位元執行檔可以在RHEL5 32和64上執行阿! 但在RHEL6 64bit上,預設沒安裝32位元的Libraries喔! 為了解決這個問題,你可以請使用者自己裝,也可以依據不同平台給不同的執行檔,這看各人取捨。如果想要讓執行檔可以同時在32與64上執行,可以參考下面說明。 假設我們有隻程式叫test.c,輸出執行檔為test。要compile成32與64的版本會使用以下command: # 64-bit version gcc -O2 -m64 -fPIC -Iinclude -o . / Release / test64 test.c # 32-bit version gcc -O2 -m32 -fPIC -Iinclude -o . / Release / test32 test.c 首先使用file指令去確認這兩個執行檔: file test64 # test64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), # dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped file test32 # test32: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), # dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped 接著用ldd指令去確認它們使用的dll: ldd test32 # linux-gate.so.1 => (0xffffe000) # libc.so.6 => /lib/libc.so.6 (0x00432000) # /lib/ld-linux.so.2 (0x00413000) ldd test64 # linux-vdso.so.1 => (0x00007fff48f...