Developing Linux programs using visual Studio 2017

Source: Internet
Author: User


Environment:


Win7_x64 flagship edition, VS2017 Enterprise Edition, VMware10.0.2, CentOS7



GCC, g++ and gdbserver need to be installed on the CentOS7, and there's not much to say


First, install the VS2017


1.1 When installing VS2017, you must check the "Linux development using C + +" toolset





Ii. Creating a Linux Project


2.1 Create a solution called "testlinux" and we'll build multiple Linux projects (including executable programs, dynamic libraries, static libraries, etc.) later in this solution






2.2 Add new Project executable program project "test"



2.2.1






2.2.2









2.3 Adding a new project to create a dynamic library



2.3.1






2.3.2









2.4 Adding a new project, creating a static library



2.4.1






2.4.2








Third, add test code


3.1 The Main.cpp code in the "Test" project:


#include <cstdio> #include "static_library/static.h" #include "dynamic_library/dynamic.h.h" int main()
{
    printf("hello from test!\n");
    printf("static_library test : %d\n", static_test(1));
    printf("dynamic_library test : %d\n", dynamic_test(1)); return 0;
}





3.2 Adding static.h and Static.cpp to the "Static_library" project



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;
}


3.3 Adding dynamic.h and Dynamic.cpp to the "Dynamic_library" project



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;
}







Iv. project configuration


4.1 "General" configuration



To configure the main program:






Local output directory: "$ (ProjectDir) bin\$ (Platform) \$ (Configuration) \" modified to "$ (projectdir). \bin\$ (Platform) \$ (Configuration) \ "is designed to place all project output files in the same directory for easy reference to each other.



The target file extension: ". Out" is modified to "", in order not to generate a file suffix, the generic Linux executable does not have an extension name, can be modified or not modified.



Remote build root: "~/projects" modified to "/root/projects/$ (SolutionName)", "~" and "/root" are equivalent, but runtime dynamic Library Search directory does not support ~ path, add "$ (solutionname)" is to differentiate between projects with the same name under different solutions.



Remote Build project directory: "~/projects" modified to "/root/projects/$ (SolutionName)", "~" and "/root" are equivalent, but run-time dynamic Library Search directory does not support the ~ path, add "$ (solutionname) "is to differentiate between projects with the same name under different solutions.



Configure Dynamic Library: "$ (Remoterootdir)/$ (ProjectName)" modified to "$ (remoterootdir)"






Local output directory: "$ (ProjectDir) bin\$ (Platform) \$ (Configuration) \" modified to "$ (projectdir). \bin\$ (Platform) \$ (Configuration) \ "



Target file name extension: ". Out" modified to ". So"



Remote Build root directory: "~/projects" modified to "/root/projects/$ (SolutionName)"



Configuration type: "Application (. Out)" modified to "dynamic library (. So)"



To configure a static library:






Local output directory: "$ (ProjectDir) bin\$ (Platform) \$ (Configuration) \" modified to "$ (projectdir). \bin\$ (Platform) \$ (Configuration) \ "



Target file name extension: ". Out" modified to ". A"



Remote Build root directory: "~/projects" modified to "/root/projects/$ (SolutionName)"



Configuration type: "Application (. Out)" modified to "dynamic library (. a)"






4.2 "Debug" configuration






Program: "$ (Remotetargetpath)" modified to "$ (remoterootdir)/bin/$ (Platform)/$ (Configuration)/$ (TargetName) $ (targetext)", Because the local output directory was modified earlier, the remote output directory also changed accordingly, and the changes are consistent here.



Working directory: "$ (Remoteoutdir)" modified to "$ (remoterootdir)/bin/$ (Platform)/$ (Configuration)", this is the remote host on the CentOS path, If the settings are incorrect, the referenced dynamic library cannot be found, and the debugger cannot start.






Other debugger commands: "" Modified to "set Solib-search-path $ (solutiondir) bin/$ (Platform)/$ (Configuration)", this is a local path, the debug symbol is loaded locally, Otherwise, when you debug the dynamic library, GDB outputs a debug symbol file that is not found .









4.3 "C + +" configuration






Additional Include Directories: In "$ (stlincludedirectories);% ( additionalincludedirectories) "Add before"./...; ", this is the path to the remote host CentOS, which is equivalent to specifying the"-i[path "option when the GCC is compiled; usually first copy the required header files from CentOS to Windows , then Set "Configuration Properties", "vc+ + Directories", "Include Directories", so that when writing a Linux program, the message is more friendly ^ ^.






4.4 "linker" configuration






Additional Library Directories: Add "$ (remoterootdir)/bin/$ (Platform)/$ (Configuration) before"% (additionallibrarydirectories) ", This is the path to the remote host CentOS, which is equivalent to specifying the "-l[path" option at GCC compile time to specify the directory referencing the dynamic and static libraries;






Library dependencies: Add "Dynamic_library;static_library", which is equivalent to the GCC settings "-l[Library name" option to specify the dynamic library and static library names required for the link, and if the dependent library file cannot be found, the link will be incorrect, showing "unresolved symbols "。






Additional options: Add-wl,-rpath=$ (Remoterootdir)/bin/$ (Platform)/$ (Configuration) to specify the path to the dynamic library when the program runs.





Five, start debugging


5.1 Setting up a remote debugging host






5.2 Displaying the Linux console









The last Demo:TestLinux.zip






Developing Linux programs using visual Studio 2017


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.