• visual studio 2015下使用gcc调试linux c++开发环境搭建完整详解


    一直以来,相信绝大部分的开发都是windows/mac下做开发,尤其是非嵌入式和qt系的,而开源服务器程序绝大部分都是跑在Linux下,几乎就没有跑在windows下的。一直以来开发人员都是在windows下调试好非平台相关部分,然后到具体环境下去调试平台相关接口。

    有着宇宙第一IDE之称的VS自从萨提亚·纳德拉上台之后,vs 2017正式支持跨平台开发,可是vs 2017听说太巨无霸了,安装好要几十个GB。而且之前只装了vs 2015,后来偶然搜到了翻译了msdn官网的帖子,vs 2015有linux c++开发的插件。太爽了。。下面来搭建一把。。

    为了使用便捷:

    1、首先确保装了小番茄;

    2、禁用字符串和注释中的拼写错误。

    3、安装visual gdb插件(远程debug应用,我都是用这个,非常好用)。

    4、安装astyle,配置格式化。

    1)、工具-》扩展和更新,搜astyle插件,下载安装重启,当前是2.0版本。

    2)、工具-》选项-》AStyle Formatter-》Edit,填入下面的,点击save,确定。

    --style=allman --indent=spaces=4 --indent-switches --indent-namespaces --break-blocks --pad-oper --unpad-paren --delete-empty-lines --convert-tabs --mode=c

    说明:

    --indent=spaces=4   行缩进用4个空格
    --indent-switches      switch 与case不同列,case缩进
    --indent-cases           缩进case下面的语句,我不喜欢
    --break-blocks           空行分隔无关块
    --delete-empty-lines  删除多余空行
    --pad-oper                 操作符两端出入空格
    --unpad-paren           移除括号两端多余空格
    --convert-tabs            tab转空格

    其次,我应该告诉你去看https://blogs.msdn.microsoft.com/vcblog/2017/04/11/linux-development-with-c-in-visual-studio/就好。

    注:我猜可能是因为垄断的原因,vs 2017自带的跨平台开发或者vs 2015的visual c++ for linux插件远不如visualgdb好用,它支持make/cmake以及各种profiler,正式开发就应该必备小番茄和visualgdb,效率倍增。本文还是以visual c++ for linux插件为例。

    第三,我需要告诉你的是msdn忽略了以及网络上找不到直接答案的关键性信息。安装好插件以及配置好ssh之后,别忙着测试,否则可以让你折腾好一会儿。

    • 卸载linux自带的gdb,下载gdb源码并编译安装gdb以及gdbserver(默认rhel Linux下不带gdbserver,一般用于嵌入式开发,否则会提示找到gdbserver);

      gdb的安装可以baidu,唯一需要注意的是,刚从gnu下载的gdb源码目录中并没有gdb/gdbserver这个目录,configure/make/make install之后,就生成了gdbserver目录,然后执行make就可以了。

    • 点击vs debug->linux console调出 linux控制台(否则,vs会卡死在那);
    • 在/etc/ld.so.conf中包含/usr/local/mpc-1.0.1/lib(否则会提示cc1plus: error while loading shared libraries: libmpc.so.3: cannot open shared object file: No such file or directory)

    第四,既然是为了在windows下开发,目标环境是linux,就一定得使用linux下专有特性验证下确实在linux下运行。

    c++代码,以获取操作系统和配置信息为例:

    #include <iostream>
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std;
    
    void getOsInfo()
    {
        FILE *fp = fopen("/proc/version", "r");
        if (NULL == fp)
            printf("failed to open version
    ");
        char szTest[1000] = { 0 };
        while (!feof(fp))
        {
            memset(szTest, 0, sizeof(szTest));
            fgets(szTest, sizeof(szTest) - 1, fp); // leave out 
    
            printf("%s", szTest);
        }
        fclose(fp);
    }
    
    void getCpuInfo()
    {
        FILE *fp = fopen("/proc/cpuinfo", "r");
        if (NULL == fp)
            printf("failed to open cpuinfo
    ");
        char szTest[1000] = { 0 };
        // read file line by line 
        while (!feof(fp))
        {
            memset(szTest, 0, sizeof(szTest));
            fgets(szTest, sizeof(szTest) - 1, fp); // leave out 
    
            printf("%s", szTest);
        }
        fclose(fp);
    }
    
    void getMemoryInfo()
    {
        FILE *fp = fopen("/proc/meminfo", "r");
        if (NULL == fp)
            printf("failed to open meminfo
    ");
        char szTest[1000] = { 0 };
        while (!feof(fp))
        {
            memset(szTest, 0, sizeof(szTest));
            fgets(szTest, sizeof(szTest) - 1, fp);
            printf("%s", szTest);
        }
        fclose(fp);
    }
    
    
    int main()
    {
        printf("===os information===
    ");
        getOsInfo();
    
        printf("===cpu infomation===
    ");
        getCpuInfo();
    
        printf("===memory information===
    ");
        getMemoryInfo();
        return 0;
    }

    再看下用户根目录下的project。

    直接在linux下运行看看是不是生成linux二进制的格式:

    大功告成,可以干活了,必须宇宙第一IDE。。。

    相关错误:

    "undefined reference to" 多种可能出现的问题解决方法,除了本文中提及的示例外,常规开发中的引用三方库等都测试过了,不如引用gperftools的时候就出现这个异常,这是因为so安装在/usr/local/lib中,但其没有定义在LD_LIBRARY_PATH中的原因。https://blog.csdn.net/shaderdx/article/details/49929147

    编译的时候找不到三方库头文件,在

    中包含头文件目录即可,VC Directories/C++中的用于本地智能提示,https://developercommunity.visualstudio.com/content/problem/40127/additional-include-folders-problems-for-linux-proj.html

    gdb提示Loaded 'shared libraries loaded at this time.'. Cannot find or open the symbol file.,如下:

    程序运行成功,不影响,这应该是gdb自己的设置问题,和vs无关。https://stackoverflow.com/questions/46127228/visual-studio-2017-for-linux-c-cannot-find-or-open-the-symbol-file

    引用非标准库中的so找不到,比如error while loading shared libraries: libmyso.so: cannot open shared object file: No such file or directory。这种情况下,需要确保包含的so在LD_LIBRARY_PATH或者系统库目录/usr/lib:/usr/local/lib下,同时加上-L编译选项,如下:

    为什么一定要同时包含呢???仔细测试下了,如果去掉IDE中的-L选项,就会提示:

    1> /usr/bin/ld: cannot find -lmyso
    1> collect2: error: ld returned 1 exit status

    可参考这篇的解释,但是感觉是其中一种原因:https://www.cnblogs.com/sylar5/p/6701318.html

    ===============================makefile工程=========================

    在实际中,linux工程几乎95%+都是使用makefile工程,剩下的一部分使用cmake(mysql),所以我们需要能够直接在VS下开发和调试makefile工程。

    1、首先创建makefile项目,如下:

    2、创建一个Makefile文件,内容如下:

    上图中的说明参见:https://stackoverflow.com/questions/48454486/debugging-makefile-project-of-linux-development-with-c

    3、设置项目属性:

    上图的说明原因参见:https://github.com/Microsoft/VSLinux/issues/99

     这样就可以调试makefile工程了。

    新补充:在有些情况下,我们没有主程序的代码,只有so的代码,此时要进行调试的话,在gdbserver模式,visual studio是step into so的代码的,无论是编译时链接的还是通过dlopen动态加载的so都一样,此时要在visual studio中进行debug,需要改为gdb模式(但是gdb模式不支持控制台交互模式,这要注意)。或者使用visualgdb,visualgdb和vs for linux在使用上还有些差别,后续专门开文讲解。

    gdb模式(gdbserver没有这个问题)控制台输出中文乱码,但是程序在linux下执行输出则不是乱码,linux下的编码格式为UTF-8。

    暂时不知道如何解决?

    vs for linux box 远程debug mysql,参考:https://mysqlserverteam.com/compiling-mysql-in-visual-studio-on-a-remote-linux-box/

    参考:

    https://blogs.msdn.microsoft.com/vcblog/2017/04/11/linux-development-with-c-in-visual-studio/

    https://blogs.msdn.microsoft.com/vcblog/2018/04/09/intellisense-for-remote-linux-headers/

    https://www.cnblogs.com/coolulu/p/4124803.html

    https://docs.microsoft.com/en-us/cpp/linux/configure-a-linux-project

  • 相关阅读:
    面向对象下
    面向对象上
    将博客搬至CSDN
    矩阵的常用术语和操作
    python2.7 Unable to find vcvarsall.bat
    intellij创建maven web项目
    intellij 编译 springmvc+hibernate+spring+maven 找不到hbm.xml映射文件
    spring Thymeleaf 中文乱码
    visualstudiocode 调试electron
    android反编译工具总结
  • 原文地址:https://www.cnblogs.com/zhjh256/p/9278533.html
Copyright © 2020-2023  润新知