参考文章:
https://www.cnblogs.com/dongc/p/6599461.html(大部分参考了这位大佬的,做了一些修改,感觉原版有一个小瑕疵)
https://blog.csdn.net/u013272009/article/details/81023594(参考了他的相关命令)
1、内容简介
VS2017下进行Linux开发时的相关
属性选项配置以及
调试方法(包含动态库及静态库实例)
2、工具
VS2017 & CentOS7或Ubuntu(虚拟机或者远程主机均可)
3、Linux环境下工具安装
Ubuntu下:
apt install -y zip
apt install -y gdb
apt install -y openssh-server
apt install -y build-essential gdbserver
CentOS7下:
yum install -y zip
yum install -y gdb
yum install -y openssh-server
yum install -y gdb-gdbserver
4、VS2017安装必备
安装VS2017时,必须要勾选“使用C++的Linux开发”工具集,如下图所示:
5、创建Linux项目
5.1 创建一个名称为"TestLinux"的解决方案,我们稍后会在这个解决方案内新建多个Linux项目(包括可执行程序、动态库、静态库等)

5.2 添加新建项目可执行程序项目"test"
5.2.1

5.2.2

5.3 添加新建项目,创建动态库
5.3.1

5.3.2

5.4 添加新建项目,创建静态库
5.4.1

5.4.2
6、添加代码
6.1 "test"项目中main.cpp代码:
#include
#include "static_library/static.h"
#include "dynamic_library/dynamic.h.h"
int main()
{
printf("hello from test!
");
printf("static_library test : %d
", static_test(1));
printf("dynamic_library test : %d
", dynamic_test(1));
return 0;
}
6.2在"static_library"项目中添加static.h和static.cpp
static.h
#ifndef _STATIC_LIBRARY_H_
#define _STATIC_LIBRARY_H_
int static_test(int n);
#endif // !_STATIC_LIBRARY_H_
static.cpp
#include "static.h"
int static_test(int n)
{
return n * 3;
}
6.3在"dynamic_library"项目中添加dynamic.h和dynamic.cpp
dynamic.h
#ifndef _DYNAMIC_LIBRARY_H_
#define _DYNAMIC_LIBRARY_H_
int dynamic_test(int n);
#endif // !_DYNAMIC_LIBRARY_H_
dynamic.cpp
#include "dynamic.h"
int dynamic_test(int n)
{
return n * 2;
}
7、属性选项配置
7.1 "常规"配置
配置主程序:

本地输出目录:"$(ProjectDir)bin$(Platform)$(Configuration)"修改为"$(ProjectDir)..in$(Platform)$(Configuration)",是为了将所有项目输出文件放到同一个目录中,方便相互引用。
目标文件扩展名:".out"修改为"",是为了不生成文件后缀,一般的Linux可执行程序是没有扩展名称的,可修改也可不修改。
远程生成根目录:"~/projects"修改为"/root/projects/$(SolutionName)","~"和"/root"是等价的,但是运行时动态库搜索目录不支持~路径,添加“$(SolutionName)”是为了区分不同的解决方案下相同名称的项目。
远程生成项目目录:"~/projects"修改为"/root/projects/$(SolutionName)","~"和"/root"是等价的,但是运行时动态库搜索目录不支持~路径,添加“$(SolutionName)”是为了区分不同的解决方案下相同名称的项目。
配置动态库:"$(RemoteRootDir)/$(ProjectName)"

本地输出目录:"$(ProjectDir)bin$(Platform)$(Configuration)"修改为"$(ProjectDir)..in$(Platform)$(Configuration)"
目标文件扩展名:".out"修改为".so"
远程生成根目录:"~/projects"修改为"/root/projects/$(SolutionName)"
配置类型:"应用程序(.out)"修改为"动态库(.so)"
配置静态库:

本地输出目录:"$(ProjectDir)bin$(Platform)$(Configuration)"修改为"$(ProjectDir)..in$(Platform)$(Configuration)"
目标文件扩展名:".out"修改为".a"
远程生成根目录:"~/projects"修改为"/root/projects/$(SolutionName)"
配置类型:"应用程序(.out)"修改为"动态库(.a)"
7.2 "调试"配置

程序:"$(RemoteTargetPath)"修改为"$(RemoteRootDir)/bin/$(Platform)/$(Configuration)/$(TargetName)$(TargetExt)",因为前面修改了本地输出目录导致远程输出目录也相应发生变化,这里修改一致。
工作目录:"$(RemoteOutDir)"修改为"$(RemoteRootDir)/bin/$(Platform)/$(Configuration)",这个是远程主机CentOS上的路径,如果设置不正确将找不到引用的动态库,调试程序无法启动。

其他调试程序命令:""修改为"set solib-search-path $(SolutionDir)bin/$(Platform)/$(Configuration)",这个是本地路径,调试符号是从本地加载的,否则调试动态库时,gdb会输出没有找到调试符号文件。

7.3 "C/C++"配置

附加包含目录:在"$(StlIncludeDirectories);%(AdditionalIncludeDirectories)"前面添加"./..;",这个是远程主机CentOS上的路径,相当于gcc编译时指定"-I[路径]"选项;一般是先把需要的头文件从CentOS复制到windows,然后设置"配置属性"->"VC+ +目录"->"包含目录",这样在编写Linux程序时,提示信息更加的友好^^。
7.4 "链接器"配置

附加库目录:在"%(AdditionalLibraryDirectories)"前面添加"$(RemoteRootDir)/bin/$(Platform)/$(Configuration);",这个是远程主机CentOS上的路径,相当于gcc编译时指定"-L[路径]"选项,用于指定引用动态库和静态库的目录;

库依赖项:添加"dynamic_library;static_library",相当于gcc设置"-l[库名称]"选项,用于指定链接时所需要的动态库和静态库名称,如果找不到依赖的库文件,链接时会错误,显示"无法解析的符号"。

其他选项:添加"-Wl,-rpath=$(RemoteRootDir)/bin/$(Platform)/$(Configuration) ",指定程序运行时搜索动态库的路径。
8、调试
8.1点击下方箭头

8.2设置远程调试主机

8.3 显示Linux控制台

OVER!
欢迎交流
源码在Git上的链接
https://github.com/GITWHB/VS2017_Linux_Develop