Share via


how to link with .a lib file in visual studio

Question

Wednesday, August 23, 2017 4:45 PM

I am trying to add an .a library file to my visual studio project. I have included the path from project properties. But it is not recognizing the classes. I know .lib files work in windows. Is there any way to use .a files with visual studio?

All replies (4)

Wednesday, August 23, 2017 4:59 PM

There could be a couple of reasons for this.

The two obvious ones are if the .a file is for a dynamic link library (.dll.a) then the format that GCC/binutils uses is different from what Microsoft uses.

Secondly, since you mentioned classes, if this is a C++ project then the way the GCC project names things is different from how Microsoft names them. This means, when you use them in your Visual Studio project, the compiler is expecting one way of naming things and when it generates code for the function calls, it will use those names. But when the linker tries to link everything together it isn't able to find those names in the libraries.

For C++ at least, name mangling isn't standardised, which is why using C function names is what is used when trying to do this.

You will have to provide more information on what you are trying to do though, but there are generally ways around this.

This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.


Wednesday, August 23, 2017 5:22 PM

Thanks Darran Rowe. Yes it's a C++ project. I am trying to add a static library (libpresage.a) in my project. I need ways to use it in visual studio.


Wednesday, August 23, 2017 6:22 PM

Unfortunately it is not possible to mix the static library with objects built with Visual Studio. In general, it is questionable mixing objects (which the static library contains) built with the same compiler using different compiler options, or even different versions of the same compiler. So using different compilers is just asking for trouble. Both compilers don't try to be compatible and they have different debug information formats and ways of doing things like exception handling.

The two best choices, if you can, are to either build libpresage with Visual Studio or to get a shared/dynamic version of libpresage. Since you are asking this question in the first place I assume that building the library with Visual Studio is not an option, so can you get a shared/dynamic version of the library?

This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.


Thursday, August 24, 2017 9:09 AM

Hi,

Welcome to MSDN forum.

To add .lib files as linker input in the development environment

  1. Open the project's Property Pages dialog box. For details, see Working with Project Properties.
  2. Choose the Input property page in the Linker folder.
  3. Modify the Additional Dependencies property to add the .lib files.

refer: /en-us/cpp/build/reference/dot-lib-files-as-linker-input

Best regards,

Joyce

Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].