1.Ubuntu VScode C++配置
參考:
https://blog.youkuaiyun.com/weixin_43374723/article/details/84064644
https://blog.youkuaiyun.com/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.youkuaiyun.com/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.youkuaiyun.com/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...