linux下產生.so檔案和.a檔案

來源:互聯網
上載者:User

test.h

1 #ifndef _TEST_H_
2 #define _TEST_H_
3
4 void TestA();
5 void TestB();
6
7 #endif

test_a.cpp

1 #include <stdio.h>
2 #include "test.h"
3
4 void TestA()
5 {
6 printf("TestA func\n");
7 }

test_b.cpp

1 #include <stdio.h>
2 #include "test.h"
3
4 void TestB()
5 {
6 printf("TestB func\n");
7 }

產生so檔案的命令

g++ test_a.cpp test_b.cpp -fPIC -shared -o libtest.so

產生.a檔案的命令

1 gcc -c test_a.cpp
2 gcc -c test_b.cpp
3 ar -r libtest.a test_a.o test_b.o

test.cpp

1 #include "test.h"
2
3 int main()
4 {
5 TestA();
6 TestB();
7
8 return 0;
9 }

採用動態庫編譯命令

g++ test.cpp -o test -L. -ltest

執行

export LD_LIBRARY_PATH=./
./test

執行結果如下。

 

採用靜態庫編譯命令

g++ -static -o test -L. -ltest test.cpp

執行效果

靜態庫的嵌套調用,有時候我想做一個自己的靜態庫,它裡面要調用其他靜態庫裡面的函數,經過實驗

這個好像用ar -r不行,所以就在連結的時候需要兩個庫檔案都包含,同時要有這一個標頭檔才行。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.