Ubuntu VScode C++ 編譯、引用.so--多專案編譯+解決No such file or directory問題
1.Ubuntu VScode C++配置
參考:
https://blog.csdn.net/weixin_43374723/article/details/84064644
https://blog.csdn.net/wangxiao7474/article/details/103164235
2. 編譯、引用.so
2.1 新建專案
```
buildSo
├─ .vscode
│ ├─ c_cpp_properties.json
│ ├─ launch.json
│ └─ tasks.json
├─ common
│ ├─ include
│ │ ├─ a.h│ │ ├─ b.h
│ │ └─ c.h
│ └─ src
│ ├─ a.cpp
│ ├─ b.cpp
│ └─ c.cpp
├─ lib
│ └─ callso
├─ testDemo
│ ├─ buildso.cpp
│ └─ callso.cpp
└─ utils
└─ buildso
├─ include
│ └─ buildso.h
└─ lib
└─ libbuildso.so
//a.h #include "stdio.h" void test_a(); //b.h #include "stdio.h" void test_b(); //c.h #include "stdio.h" void test_c(); //a.cpp: #include "a.h" void test_a() { printf("this is in test_a...\n"); } //b.cpp: #include "b.h" void test_b() { printf("this is in test_b...\n"); } //c.cpp: #include "c.h" void test_c() { printf("this is in test_c...\n"); } //buildso.h: #include "stdio.h" #include "a.h" #include "b.h" #include "c.h" void test_buildso(); //buildso.cpp: #include "buildso.h" void test_buildso() { test_a(); test_b(); test_c(); printf("this is in test_buildso...\n"); } //callso.cpp #include "stdio.h" #include "buildso.h" int main() { test_buildso(); test_a(); }
2.2 修改編譯、引用.so 的配置檔案
(1)Ctrl+shift+D --> 點選執行和除錯 --> 選擇"C++(GDB/LLDB)" --> 選擇“g++ -生成和除錯活動檔案“ --> 生成“launch.json”和“tasks.json”檔案;
(2)Ctrl+Shift+P --> 開啟搜尋框,鍵入c++ --> 選擇“Edit configurations (JSON)“ --> 生成“c_cpp_properties.json”;
參考:https://blog.csdn.net/cdknight_happy/article/details/106574300
task.json:
// ************buildso,生成so包時的配置檔案*************
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g","-std=c++11",
"${workspaceFolder}/common/src/*.cpp", //so包函式實現的檔案
"${workspaceFolder}/testDemo/buildso.cpp",
"-o",
"${workspaceFolder}/utils/buildso/lib/libbuildso.so", //生成的包一定要是lib+包名+.so的形式
"-fPIC", //生成和位置無關的so檔案
"-shared", //生成動態連結庫,即so檔案
"-I", "${workspaceFolder}/common/include",
"-I", "${workspaceFolder}/utils/buildso/include"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
// ************callso,呼叫so包時的配置檔案*************
/*
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g","-std=c++11",
"${workspaceFolder}/testDemo/callso.cpp", //so包函式實現的檔案
"-o",
"${workspaceFolder}/lib/${fileBasenameNoExtension}",
"-I", "${workspaceFolder}/utils/buildso/include",
"-I", "${workspaceFolder}/common/include",
"-L", "${workspaceFolder}/utils/buildso/lib", //庫檔案路徑,多個路徑時逗號分割
"-l", "buildso" //具體的庫檔案,這裡是指libhello.so
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
*/
fPIC的含義,請參考:https://www.cnblogs.com/fengliu-/p/10216878.html
shared的含義,請參考:https://www.cnblogs.com/ziyunlong/p/6023121.html,就是建立動態連結庫,對應於windows下的dll,所以在執行程式的時候的時候要通過系統的環境變數能找到對應的so檔案。
lauch.json:
// ************buildso,生成so包時的配置檔案*************
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以檢視現有屬性的描述。
// 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和除錯活動檔案",
"type": "cppdbg",
"request": "launch",
"program": "{workspaceFolder}/utils/buildso/lib/libbuildso.so",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "為 gdb 啟用整齊列印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
// ************callso,呼叫so包時的配置檔案*************
/*
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以檢視現有屬性的描述。
// 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和除錯活動檔案",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/lib/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "為 gdb 啟用整齊列印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
*/
c_cpp_properties.json:
// ************buildso,生成so包時的配置檔案*************
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/common/include",
"${workspaceFolder}/utils/buildso/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
// ************callso,呼叫so包時的配置檔案*************
/*
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/utils/buildso/include",
"${workspaceFolder}/common/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
*/
2.3 配置Linux動態連結庫搜尋路徑
編譯生成.so檔案之後, 引用.so,若直接F5執行,會出現如下問題:
/home/morgandas/VScodeProjects/testSo/lib/main: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory
解決辦法:
參考:https://blog.csdn.net/yjk13703623757/article/details/53217377
1.將使用者用到的庫統一放到一個目錄,如 /usr/loca/lib/vscodeDevelop
# cp libXXX.so.X /usr/loca/lib/vscodeDevelop2.向庫配置檔案中,寫入庫檔案所在目錄
# vim /etc/ld.so.conf.d/usr-libs.conf
/usr/local/lib/vscodeDevelop3.更新/etc/ld.so.cache檔案
# ldconfig4. 重啟系統生效
採用這種辦法,對於原系統的改動也最小。而/etc/ld.so.conf的檔案內容是include /etc/ld.so.conf.d/*.conf,所以在/etc/ld.so.conf.d目錄下,加入任何以.conf為字尾的檔案,都能被ld識別。
2.4 測試
按F5執行,呼叫成功。
this is in test_a...
this is in test_b...
this is in test_c...
this is in test_buildso...this is in test_a...