vs compile a Linux project generate static libraries and statically link methods in another project

Source: Internet
Author: User

VS2017 also launched a long time, in the unit when the Linux server program can only use VIM, which makes use of the IDE I am very uncomfortable.

Plus, you want to roll a set of Linux wheels and decide to use vs start to write debugging Linux programs remotely.

Writing a static library under Windows and referencing it in another project is simple

When you create a new project, you can compile the static library by selecting the static library and *.lib the code.

Then, in the project that you want to use the static library, set the linker "Additional Library directory" to ". /debug "(related to your output path), and then add the" Additional dependencies "static library name. Lib, then you can compile the static library successfully.

But under Linux it's been a headache for me for a few days, and I was prepared to follow the Windows routines to fix the option of not seeing the static library when I was creating a new project ...

But fortunately, the "Configuration Type" option is found in the general, and is set to "Static library (. a)".

When preparing to use the static library, the real trouble comes, at first I follow the idea of Windwos, the current project link when the working directory is the solution path, so according to the compiled file structure wrote "Additional Library directory" (. /common/bin/x64/debug), and then go to fill in the "Additional Dependencies" ("Libcommon.a"), the compilation of the time on the gorgeous Li error, prompted not to this file ...

Later tried to "Libcommon.a" to "common", "Additional library directory" directly designated as the absolute address on Linux, and so on, but unfortunately there is no one program can.

Under Linux to write their own parameters to specify the absolute address can successfully link the static library, but with the environment of VS always failed, this time a bit of my doubts about life.

Even once wanted to write good code with VS and then go to Linux to write makefile, manually compiled. But then again, is this not going back to ancient times? No, I have to get rid of the problem.

Later on the whim, wrote a small program myhelp, put into the usr/bin/

intMainintargcChar**argv) {    Charbuf[ the]; GETCWD (BUF,sizeof(BUF));    Std::ofstream F; F.open ("/home/reskai/work.txt"); if(f) {f<<buf; F<<"\ r \ n";  while(argc-->0) {f<< *argv++ <<"\ r \ n";    } f.close (); }    return 0;}
View Code

The myhelp is used to get the current working directory and to output all the parameters of the call to the file.

Then change the linker of the project, replace the g++ with Myhelp, and get some interesting information.

/home/reskaimyhelp-o/home/reskai/projects/projectk/bin/x64/debug/projectk. out-wl,--no-undefined-wl,-l. /common/bin/x64/Debug-wl,-z,relro-wl,-z,now-wl,-z,noexecstack/home/reskai/ projects/projectk/obj/x64/debug/MAIN.OLibcommon.a

When calling Linux remotely, the current working directory is not a project path but a ~/... Well...... That's why I took it for granted that I hadn't been able to compile successfully.

Then I changed the "Additional Library directory", set it to the correct location, and then re-link ... I doubted my life again and still couldn't find the file. (In fact, should think of this time, before I set the additional library directory as an absolute address still cannot link to explain this option is still a bug)

In the end, I tried to delete the "Additional library directory" and then fill in the "Additional dependencies" with the full Static library path "./projects/common/bin/x64/debug/libcommon.a", this time finally through the link.

Although finally successfully with VS in Linux statically linked to their own library, but this solution, a bit nondescript. I do not know whether the vs2017 bug or my Linux compiler link system is not very clear.

Use this article to document the problem and the way of thinking and the final solution I have encountered.

Off-topic, because this problem let me before the day before 4 o'clock in the morning fall asleep, the second Sky a day class, night again think this problem to now this point to solve.

vs compile a Linux project generate static libraries and statically link methods in another project

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.