sunset
sunset

Reputation: 1413

why can't i include a static library into a cpp file?

Is there a way to #include "library.a" or it's .so or it's .o inside a cpp or cc file?

I would like to do this because I am using this file inside another file.

Upvotes: 0

Views: 3800

Answers (5)

Marcus
Marcus

Reputation: 153

The #include statement basically just includes other source code into the current file. That being said, a static library is not source code and cannot be included in this manner. Static (and shared) libraries are instead linked into the project after all the compiling is done.

What you have to do is to include a file containing prototypes to the functions you are going to be using. This way the compiler knows it is there and the linker will sort out the rest.

For more information on how to create and link static/shared libraries, check out this page.

Upvotes: 1

Puppy
Puppy

Reputation: 146910

You can do this in the Visual C++ compiler, using #pragma comment(lib, "libname")- and the similarity is somewhat dubious. However you will have to discover any alternatives for your own favourite compiler.

Upvotes: 2

Dan
Dan

Reputation: 13382

#include is used for telling the compiler about the functions from a library which you will call in your code, that is to say it includes C++ code. Generally this takes the form of header files which have the function declarations in them.

.a and .so and .o files are compliled code which can be linked into your compiled code using the linker.

Edit: there's an introduction about compiling and linking here

Upvotes: 1

tonekk
tonekk

Reputation: 478

It simply doesn't make sense.

You include the library's code at compile time by linking it.

Normally there's a header file for the library you can include.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385114

#include is for C++ code.

.a, .so and .o files are not C++ code.

It's more likely that you want to #include a C++ header file (typically ending in .h or .hpp), and link an object file.

Upvotes: 4

Related Questions