LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Linking static and shared libraries in the KDevelop Environment (https://www.linuxquestions.org/questions/programming-9/linking-static-and-shared-libraries-in-the-kdevelop-environment-374624/)

tristanm 10-19-2005 07:13 AM

Linking static and shared libraries in the KDevelop Environment
 
Right here goes...

I am using the KDevelop environment to write simulation software on SuSE 9.2 Pro. I am using third party libraries for the mathematics and would lke to do so for the plotting.
The math library is static(.a). The only way I can get it to link is by placing its full path in "Project -> Project options -> Configure Options -> Linker flags (LDFLAGS):". I then include the header files by using the Automake Manager and right clicking "src" selecting the "Includes" tab and adding the relevant directory to the "Directories outside project" pane. This works fine.

I also tried another method of linking the static library: in the Automake manager I right click on the program and select "Options". I then select the Libraries tab and add the full path of the library to the "Link libraries outside project (LDADD)" window. When I try and compile with this method the error messages stating "undefined reference to ..." dissapear and instead I get "Exited with status: 2".

The exact same thing happens when I try to link a shared library in this fashion. However I cannot seem to add multiple libraries to the Linker flags box as described above. I have tried space, coma and semi-colon seperated lists with no success. I get "Exited with status: 77" from autoconf.

I have no idea what to do now and am intensly frustrated. I need to link more than one library and just cannot seem to do it. The documentation for Kdevelop is very poor being significantly incomplete!

I would appreciate some help on this matter. Am I going about the linking the correct way or is there a better way to do it?

Regards
Tristan

cppkid 10-20-2005 01:24 AM

First of all i know that this is a problem in KDevelope3, but you can do it.
In Kdevelop3 on the left of you kdevelope editor there is an menu for automake menager, go to that menu and go to options by left click on your project. on the first tab i.e Flags there are some check boxes and then there you will find a field with text:
other $(KDE_RPATH) $(all_libraries)


just append it with the path of your libs and it will work fine. for example:
other $(KDE_RPATH) $(all_libraries) ../../MyLib.a ../../StaticLib.a

and do not use semi-colon or comma to seperate multiple libraries. Just use space to seperate two libs.

wish you all the best!!

cppkid 10-20-2005 01:38 AM

If you didn't find it you can do it manually by.
You can also do it by changing the make file, After compiling the project go to the debug or optimized folder whatever you configure for, and open the make file. and then search for macro LIB and you will find it nearly about line # 310 to 315, you will find it as:
LIB =
just wite your libs with reletive path and run your project, like.
LIB = ../../libs/myLib.a ../../libs/math.a ../LastLib.a

Again keep in mind that do not use comma or semi-colon. Just use space to seperate two libs.

tristanm 10-20-2005 03:51 AM

Thanks for the help cppkid. Your first solution works great for static libraries. Now I am trying to link shared libraries and once again I get left with "Exited with status:2". I wonder if perhaps I am missing some of the libraries which should be linked. But if this was the case, wouldnt the compiler tell me where the errors lie?

tristanm 10-21-2005 05:28 AM

I have fixed the library linking problem. The solution I found was as follows:

In the Automake Manager pane called "Link Libraries Outside of Project": the full path of a static library including the file name and extension should be added without any preceding "-l".

To link a shared library one should add the path to LDFLAGS as follows: "-L/path/to/library". Then one should add the name of the library to the "Link Libraries Outside Project" pane as "-lnameoflibrary". This should not contain the lib prefix or .so extension.

Automake Manager then constructs the line in the makefile which will look as follows:
LDFLAGS = /path/to/static.a -L/path/to/shared -lshared

Hope this helps anyone else out there who may have experienced this problem.

Cheers
Tristan

punx120 05-12-2006 03:10 AM

Hello,

I'm also trying to link static and shared libraries.

In my kdevelop project, I create 4 libtool sub project. The problem is that, those libraries are dynamically linked, and I don't know how to link statically.

I tried to add .la files (generated y libtool) to the command lines, but libtool told me that it couldn't find those files.


Thanks for your help.

tristanm 05-12-2006 11:25 AM

I am not really sure what your problem is punx120. Please describe specifically what you are trying to do and what you have done that won't work. Are the libtool libraries part of your project or are they 3rd party? What command lines are you adding them to?

I have not really tried linking libtool libraries. I generally just link static libraries with the .a extension in which case you need to specify the full path as I have already noted. Don't forget that all you are really doing is creating a makefile with a gui. If you are not sure where to add something in the automake gui in kdevelop, then I suggest that you add it and take at the look at the makefile which is generated. Knowing where it ends up in the makefile should help enourmously.

A query for anyone else out there reading this thread... Has kdevelop made the compiling and linking of your own shared libraries any easier lately? The last time I tried was at the end of last year and I wasn't very succesful. All I managed to do was create static libraries with .so extensions! :rolleyes:

punx120 05-13-2006 05:24 AM

Actually, I create a simple C++ project. Then I'd like to make sub directory, so I find the solution to add new targets (libtool library). It works well. But it doesn't create classic .a file, but .la (libtool .a I guess). And as you said, I create static libraries with .so extensions !

So I can't find any solutions, I will take a look to the Makefile this afternoon.

tristanm 05-14-2006 02:00 PM

There should be two library options. A library and a libtool library. Select just the library to create a .a static library.

punx120 05-14-2006 02:24 PM

Thanks, I found the good target.

muttley.82 10-30-2006 04:34 AM

Hi, i've the same problem with a kdevelop C++ project organized in more than one directory.
I associate a library no-inst target(.a) for each subdirectory but at linking time of the project i receive these errors:

...
classX::methodsY() undefined reference to classZ::methodsW()
...

where classX and classY reside in two different subdirectory.

Thank, Antonio.

punx120 10-30-2006 04:42 AM

Are you sure to link with all static libs ?

xhi 10-30-2006 08:48 AM

Quote:

Originally Posted by muttley.82
Hi, i've the same problem with a kdevelop C++ project organized in more than one directory.
I associate a library no-inst target(.a) for each subdirectory but at linking time of the project i receive these errors:

...
classX::methodsY() undefined reference to classZ::methodsW()
...

where classX and classY reside in two different subdirectory.

Thank, Antonio.

did you tell your project to link the libraries into it? you have to specify that those libs are linked in, because it is not done automatically..

i cant remember off the top of my head but its and option on the automake config panel somewhere..

tristanm 10-30-2006 10:07 AM

Originally posted by muttley.82:
Quote:

Hi, i've the same problem with a kdevelop C++ project organized in more than one directory.
I associate a library no-inst target(.a) for each subdirectory but at linking time of the project i receive these errors:
Er... Its been a while since I last worked with this stuff but I'll try to help.

First of all, whay are you making your libraries no-inst targets? I have no idea how the no-inst tag really works but I have never used it for libraries before (not even for static libraries).

Secondly: right click on the project targets for the main project and each sub-project. Under the libraries linker make sure that there are internal links to all the internal libraries necessary for that sub-project to compile. Also right click on the project tree and make sure that all the internal include directories are setup in much the same way.

If my explanation is unclear or you continue to have trouble I will try and give more precise directions or post some screen shots (if I can do that here).

nhatkhang 11-27-2006 12:33 AM

hi ! sorry for the lenght of this post.
i'm using KDevelop 3.3.1 on Ferdora Core 5
i create a C project with Automanke Manager, add main.c ,LibBkTrans.h into it.
//main.c
#include "BkTransLib.h"
int main() {
if (BKInit())
{
//do nothing
}
}
//BkTransLib.h
int BKInit();

a shared library libbktrans.so.0.0 contains the implementation of BKInit(), i put it in /usr/local/lib and install it correctly
i build the project --> error: "undefined reference to BKInit(), it's because i haven't linked against the shared library
so i right-click on the project name (Program in bin)in Automake Manager window, choose Options, in the tab Libraries i add
-lbktrans into the box "Link libraries outside the project(LDADD)"
at last i rebuid the project --> Success

now i delete file /path_to_project/src/Makefile.in and the whole directory /path_to_project/debug
then i remove the main.c from the project, go to /path_to_project/src and rename main.c to main.cpp, no problem, cause the main.c is very simple like above.
i add this main.cpp into the project and rebuid --> error: "undefined reference to BKInit()"
i don't know why.

i try another approach: create a C++ project, add main.ccp and BkTransLib.h into it
build --> error: "undefined reference to BKInit()"
i link the library by adding -lbktrans into the box "Link libraries outside the project(LDADD)"
rebuild --> error: "undefined reference to BKInit()".

perhaps, the shared library can't link into a C++ project.(this is my very stupid idea !)
because i was successful to link against a simple shared library to both C and C++ project.
does anyone have a better idea ?
Regards
nhatkhang


All times are GMT -5. The time now is 08:40 PM.