g 的編譯選項 Wl, rpath

2021-06-02 12:41:14 字數 622 閱讀 1789

假設main.cpp,hello.h,hello.cpp,其中main.cpp呼叫了hello類中的方法

1 生成hello.so

g++ -shared hello.cpp -olibhello.so

2 編譯main.cpp,並鏈結,並指定執行時libhello.so的位置

g++ main.cpp -lhello -l./ -wl,-rpath=./ -o main

值得一提的是,如果採用帶版本號的庫,例如libhello.so.2

鏈結命令可使用g++ main.cpp libhello.so.2 -l./ -wl,-rpath=./ -o main

2)加入第二個so庫

g++ main.cpp -l./second/ -wl,-rpath=./second/ -lsecond -l./hello/ -wl,-rpath=./hello/ -lhello  -o main

ps,遇到過乙個奇怪的問題,就是假設libhello.so還用到了libother.so,由於在/etc/ld.so.conf裡配置錯誤了libother.so的目錄路徑,導致一直產生undefined reference to錯誤,但是在工程裡對libother目錄路徑配置是正確的,有可能於查詢路徑順序有關

cmake中新增 g編譯選項

在cmake 3.12之前,新增編譯選項可以如下方式新增 add definitions wall g 新增了之後,就相當於在編譯的時候加上了 wall g選項 沒加之前 gcc c main.c o test 新增之後,相當於 gcc g wall c main.c o test書中給出的示例如下...

C語言 g 常用編譯選項備忘

不加引數 gcc test.c 生成可執行檔案 a.out 預設生成可執行檔案 o 生成指定的檔名 例子 gcc o test test.c 生成可執行檔案 test c 生成目標檔案 例子 gcc c test.c 生成目標檔案 test.o c o 生成指定檔名的目標檔案 例子 gcc c o ...

01 g 編譯器常用選項

gcc gnu compiler collection gnu編譯器套件集合 gcc gcc中的c編譯器 g gcc中的c 編譯器 eg e hello.cpp o hello.i 預處理 preprocessing 生成.i檔案 sg s hello.i o hello.s 編譯 compilat...