Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » How to link a static library in C++?
How to link a static library in C++? [message #1716628] Sun, 06 December 2015 19:03 Go to next message
Simone Sabato is currently offline Simone SabatoFriend
Messages: 8
Registered: December 2015
Junior Member
Hi to everyone
I'm new in this community and I have a problem. I created a static library and I tried to link it without any success. I can't find anything on internet that could help me out.

I followed these steps to link the library:

Properties -> C/C++ Build -> Settings -> GCC C+ Compiler -> Includes

In Include paths I only added the path of the folder of library.

Then in GCC c++ Linker:

In Libraries I added the name of library;
in Library search path I added the path of the library.

What's the matter?

Thanks! Smile
Re: How to link a static library in C++? [message #1716714 is a reply to message #1716628] Mon, 07 December 2015 16:54 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
It's not a C++ thing. It depends on the linker being used.

Assuming you are using gcc (or g++) set the -static option in the command doing the linking.
For CDT, go to Project ==> Properties ==> C/C++ Build ==> Settings and add -static to the linker command. There may be a selection box for it, If not add it to the command.

Another option is to place the full name of the library in the command as if it were an object file and without the -l option.

GNU linker (ld) options:
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

Again, I am assuming GCC which is most likely. If you are using Visual Studio or others there are similar methods but you will need to consult their documentation.

[Updated on: Mon, 07 December 2015 17:08]

Report message to a moderator

Re: How to link a static library in C++? [message #1716809 is a reply to message #1716714] Tue, 08 December 2015 11:03 Go to previous messageGo to next message
Simone Sabato is currently offline Simone SabatoFriend
Messages: 8
Registered: December 2015
Junior Member
Hi David,
First of all thanks for your reply. I tried to do what you told me but the compiler still shows me error like "can't find -lnameoflibrary".

Please can you tell me step by step what i have to do to link a static library?

I use eclipse cdt mars.

Thank you again Smile
Re: How to link a static library in C++? [message #1716854 is a reply to message #1716809] Tue, 08 December 2015 15:26 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Well, you might think you have but maybe not. There isn't enough information to diagnose your problem. Can you show the link command displayed in your build log along with the error message? They should be close to each other in the build log. You could cut and paste or take a screenshot of it.

What is the name of the library ? Also, does the library actually exist?

-lXXX is converted to libXXX.a or libXXX.so and the path is prepended. The static one ends in .a. If your library doesn't follow that naming convention then you can't use -l and instead must place the the full name on the command line.

I have been assuming you are compiling and linking for Linux.
If not say so.

Re: How to link a static library in C++? [message #1716880 is a reply to message #1716854] Tue, 08 December 2015 18:21 Go to previous messageGo to next message
Simone Sabato is currently offline Simone SabatoFriend
Messages: 8
Registered: December 2015
Junior Member
Ok I described you all that I made.

First of all in eclipse I created a new C++ project. I selected static library, empty project, called the project name lab1 (as it's an exercise for university) and I selected the toolchains "Linux GCC". Then I created two files: array.h, where I wrote the prototype of functions, and array.cpp where I implemented my functions and I included array.h ( #include "array.h"). I built the library and all it's ok as the compiler doesn't show me error.
So I created another project called "test_lab1.cpp" where I'd like to test the functions that I implemented in my library. I included the library (#include "array.h") and I made all that i could make in project properties to link the library in test_lab1.cpp but now the compiler shows me error that it can't find the library.

So I ask if you can tell me what I have to do to link my library.

I'm attaching you a screen of the error. I hope you can understand the language because It's written in Italian.

Thanks again! Smile
Re: How to link a static library in C++? [message #1716907 is a reply to message #1716880] Wed, 09 December 2015 00:45 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
OK, Simone, just to be sure, the name of the library you are looking for is libarray.a ? If it isn't you can't use -larray to link it.

I still need to see what the link command actually is being used. It should be near the bottom of the build log which should be appearing in a console view during the build.

Typical build log in a console view:
index.php/fa/24223/0/
Note how I specified obj/nblib.a and obj/locallib.a. I had to do this because they don't follow the libXXX.a naming convention. I create my own makefiles. Specifying lib files in CDT is sometimes tricky as the position order can be significant. I don't think it is in your case though.

The others could have been specified using -L and -l, for example: /home/dvavra/proj/PNL/high/source/.libs/libhigh.a could have been specified by
-L/home/dvavra/proj/PNL/high/source/.libs
-lhigh


Please post the full path to your library as well as its complete name.

You say you built the library so you must know the full path to it.

My guess is (if the name is libarray.a) you haven't provided the proper path with the -L option. The build command will should have it. GCC will not complain about incorrect path names.

Since it is in another project you can use the the Worlspace... button to specify a file in the current workspace (assuming both projects are in the same workspace and the project files are in the workspace).

When you build. you should see -L<path-to-libarray.a> -larray on the linker line with <path-to-libarray.a> as the actual path.

index.php/fa/24222/0/

EDIT:
Expanded some paragraphs.

[Updated on: Wed, 09 December 2015 01:06]

Report message to a moderator

Re: How to link a static library in C++? [message #1716951 is a reply to message #1716907] Wed, 09 December 2015 09:21 Go to previous messageGo to next message
Simone Sabato is currently offline Simone SabatoFriend
Messages: 8
Registered: December 2015
Junior Member
Hi David,
So to specify the position of my library I used the workspace... button.
I'm attaching screens about the properties of linker, the properties of library and the console when I build the project.

Thanks Smile
Re: How to link a static library in C++? [message #1717007 is a reply to message #1716951] Wed, 09 December 2015 15:08 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Simone,
The link command looks correct..

Open a command window and list the contents of /home/simone/workspace/lab1/Debug
The linker is trying to find /home/simone/workspace/lab1/Debug/libarray.a
Does this file actually exist? I don't think it does.


I think the confusion is that the default name of the library is libPROJECT_NAME.a and you are assuming it will be libarray.a.

I made a static test library project called HelloC++Lib with the single function Afunc.
The library produced was named lib HelloC++Lib.a

These are the contents of the Debug directory in /HelloC++Lib:
[dvavra@fred ~]$ ls ~/workspaces/workspace/HelloC++Lib/Debug/
Afunc.d  Afunc.o  libHelloC++Lib.a  makefile  objects.mk  sources.mk  subdir.mk


If the project is named lab1, the library name will default to liblab1.a
Try changing the linker command in your test project to -llab1


Re: How to link a static library in C++? [message #1717039 is a reply to message #1717007] Wed, 09 December 2015 17:28 Go to previous messageGo to next message
Simone Sabato is currently offline Simone SabatoFriend
Messages: 8
Registered: December 2015
Junior Member
Hi David,
so in a command window I've seen the content of debug folder and in fact there isn't libarray.a but liblab1.a .
So finally what i have to do?

I'm attaching you the screen of the command window

Thanks Smile
Re: How to link a static library in C++? [message #1717041 is a reply to message #1717039] Wed, 09 December 2015 17:42 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Go to Project ==> C/C++ Build ==> Settings an on the Tool Settings tab select GCC C++ Linker ==> Libraries and in the Libraries (-l) pane change array to lab1.

index.php/fa/24253/0/
  • Attachment: Lab1.png
    (Size: 46.56KB, Downloaded 47101 times)
Re: How to link a static library in C++? [message #1717054 is a reply to message #1717041] Wed, 09 December 2015 18:02 Go to previous messageGo to next message
Simone Sabato is currently offline Simone SabatoFriend
Messages: 8
Registered: December 2015
Junior Member
I've made it,
but now the compiler shows me another error.
Re: How to link a static library in C++? [message #1717065 is a reply to message #1717054] Wed, 09 December 2015 18:49 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Looks like it's a template function. You have to instantiate these somewhere.

Here's an example where I've instantiated Which<T> for STRING, int, UINT, float and double:
template<class T>
int Which(T val, const std::vector<T> & list);

template<class T>
int Which(const T nam, const std::vector<T> & list) {
	for (UINT i=0; i<list.size(); i++) {
		if (nam == list[i]) return i;
	}
	return -1;
}

template int Which(STRING, const std::vector<STRING> &);
template int Which(int,    const std::vector<int>    &);
template int Which(UINT,   const std::vector<UINT>   &);
template int Which(float,  const std::vector<float>  &);
template int Which(double, const std::vector<double> &);


Re: How to link a static library in C++? [message #1717075 is a reply to message #1717065] Wed, 09 December 2015 19:45 Go to previous messageGo to next message
Simone Sabato is currently offline Simone SabatoFriend
Messages: 8
Registered: December 2015
Junior Member
Sorry I don't understand the problem.
I post the code of function and the prototype. Can you correct them?

Thank you Smile

int greaterThan(vector<T> v, T k);


template <typename T>
int greaterThan(vector<T> v, T k){
	int c = 0;
	for (typename vector<T>::iterator it = v.begin(); it != v.end(); it++)
		if (*it > k)
			c++;
	return c;
}
Re: How to link a static library in C++? [message #1717093 is a reply to message #1717075] Wed, 09 December 2015 21:24 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
The lines saying

template <typename T>
int greaterThan(vector<T> v, T k){
...
}


just tell the compiler how the function should be built. It is a kind of declaration similar to the prototype you placed in your header file. Think of it as a kind of macro. It doesn't generate any code by itself. This definition has to be available for the compiler to actually build the function from the template using the desired type for T.

If the function prototype is declared in a header file, but the function body template is in a separate file, then the compiler wouldn't know how to implement it when you actually try to use it and will assume it has been defined and expamded externally.

So, when they are separate, you need to actually expand it for every type you intend to use. You do this with a line for each type. For example, for int:
template int greaterThan(std::vector<int> , int);

The line has to appear in the same file as the body template and the object for that file must be linked with the rest of your code.

Another way to handle this is to place the template for the body in the same header file as the prototype. Then it will be expanded whenever it's used. This could result in multiple copies. A lot of the std library is built this way.

There are other ways to handle this.
https://isocpp.org/wiki/faq/templates
http://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file




Re: How to link a static library in C++? [message #1717185 is a reply to message #1717093] Thu, 10 December 2015 14:58 Go to previous message
Simone Sabato is currently offline Simone SabatoFriend
Messages: 8
Registered: December 2015
Junior Member
Hi David,
I resolved all Smile
I guess I must study more C++ XD. I think It's more difficult than C !

By the way thank you very much Very Happy
Previous Topic:how do you compile eclipse itself?
Next Topic:How do I run custom Codan static checker on selected files
Goto Forum:
  


Current Time: Sat Apr 20 04:12:57 GMT 2024

Powered by FUDForum. Page generated in 0.04525 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top