Saar Arbel
Saar Arbel

Reputation: 111

How do i call methods from .A file in C project?

I've got file with .a extention, and file.h that represent the functions in the .a file.

How can i call the functions in the .a file from a new C project?

I am using Visual studio, and i serached all over the internet and didn't found something helpful.

Thanks

Upvotes: 0

Views: 478

Answers (2)

Aaron Burke
Aaron Burke

Reputation: 526

There are two steps to using an external library in your Visual Studio project: you need to satisfy both the compiler and the linker.

Compiler

This is as simple as including the header file in your application; either put the header file directly in your source tree, or, if it lives in another directory, add it to your include path (Right-click on the project->Properties, choose "C/C++" and add the directory to the "Additional include directories" property).

You should now be able to #include "myexternallibrary.h" without problem, which will allow the compiler to recognize the library functions.

Linker

Generally speaking, to satisfy the linker, the instructions are very similar to the header file above: dump the library binary directly into your project tree and reference it via Properties->Linker->Input->"Additional Dependencies" (e.g., mylibrary.a); if the library lives in a directory external to your project tree and you don't want to include the full path to the library in this property, you can add the directory to the search path of the linker: Properties->Linker->General->"Additional Library Directories".

This next bit is where things get a bit trickier since you explicitly stated the library file was a .a format, and not .lib.

.a library files typically result from the MinGW toolchain, which may or may not work correctly with your Visual Studio toolchain. See related for details: From MinGW static library (.a) to Visual Studio static library (.lib)

You might get lucky and the library's usage of the standard C runtime is compatible between MSVC and MinGW (or whatever toolchain the .a file was generated with).

As a side note, please ensure that the library you're attempting to use is not actually a Linux library, as the .a library extension is also the default for GCC (the most common C compiler for Linux), and it will not be compatible in a Windows environment.

Upvotes: 1

FredK
FredK

Reputation: 4084

#include 'xxx.h"

Then in the code, supposing ABC is a function in the library, just call it: int x = ABC();

You also have to link your program with the library.

Upvotes: 1

Related Questions