Link to home
Start Free TrialLog in
Avatar of akshayxx
akshayxxFlag for United States of America

asked on

urgent .. linking libraries

i m facing a weird problem ..
working on redhat 7.3.. ( if this is of any relevnce)
gcc version 2.96

i got bunch of some source codes and header files
i compile them .. make object files
take object files and make static library

using ar -rc libmystatic.a *.o

i copy the headers to some directory
copy the lib to some directory
run ranlib on the library

then i make a sample program which uses the functions
that were in some files included in my static library

now i issues this
gcc -o myprog -I/A/B/C/myInclude -L/A/B/C/mylib -lmystatic -lpthread myprog.c

i get it compiled easily..
but while linking
ld returns with error with all used library function as undefined reference like this
.
.
.
.
/tmp/cc428bIK.o(.text+0x2bf): undefined reference to `getExceptionStackTrace'
collect2: ld returned 1 exit status


i understand that somehow the library is not getting linked .. but i am not able to figure out why is that .. can u help me on this .. its urgent ..am i missing some step while installing the custom static library
Avatar of akshayxx
akshayxx
Flag of United States of America image

ASKER

ok no need . figured it out
but still i found the reason weird
first one to comment will get the points .. or tell me how to delete it
gcc -o myprog -I/A/B/C/myInclude -L/A/B/C/mylib -lmystatic -lpthread myprog.c

ok explain this .. and get the points ..
i changed this to
gcc -o myprog -I/A/B/C/myInclude -L/A/B/C/mylib myprog.c -lmystatic -lpthread
and it worked ..
though the previous command works on AIX
Avatar of F. Dominicus
The explanation is not as hard if you think along the lines who needs what.

myprog.c needs symbols from mystatic which needs symbols from lpthread. There the order of inclusion of static libraries is not free!

Here's a minimal example (just with a total simple static library which just contains this:

#include <stdio.h>

void hello (void){
  puts("Hello");
}


Now a as simple main.c
int main(void){
  hello();
  return 0;
}


Now I try to linkt that stuff this way:
gcc -L. -lhello main.c  
/tmp/ccS49N1j.o(.text+0x11): In function `main':
: undefined reference to `hello'
collect2: ld returned 1 exit status

Ah well does not work!
Now in the "right order"

 gcc -L. main.c -lhello
/q2% ./a.out
Hello

That it in the first way the symbol for hello can not be found in the second without problems. That's a bit of a problem with linking. You must put them add the end of you r command line and you must put them in the right order.

Regards
Friedrich
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well ...in this case i dont think its the need basis .. GCC should handle that .. it should load all the commandline arguments and rearrange them accordingly ..
 
What does points refundes mean? and why is the question closes from Netminder?
Well you might think it should but for that gcc needs a database for all the symbols and in which directory they can be found. That won't happen on Unix.

Friedrich
for rearranging may be it wont require database .. it can take all the linking options ...and move them to appropriate order..
and for now i am not able to figure out .. if there can be any problem in identifying the linking options .. from other options
Avatar of Netminder
Netminder

fridom,

"Points refunded" means that the points offered by the Asker for a solution to the question have been refunded to him, since he solved the question himself and posted the solution.

The reason it says it's been closed by me is because as a Moderator, I closed the question. Had there been some Expert whose comment had been selected as an answer, then instead of a 0 point value, you would have seen a number equaling ten per cent of the original value of the question (e.g. 10 points means the original question was worth 100 points to the Expert who best solved the problem), and you would have seen that Expert's comment with a green stripe instead of mine.

Netminder
CS Moderator
I may be picky but I don't think that's correct. He wrote:
gcc -o myprog -I/A/B/C/myInclude -L/A/B/C/mylib -lmystatic -lpthread myprog.c

ok explain this .. and get the points ..
                               ^^^^^^^

If I explain it the points are for me. Well I explained that in details that the order in which the library are included in the linking *is* important.


Regards
Friedrich
fridom,

I can't tell one way or the other whether you explained it; I'm not a C programmer.

However, that isn't the way Experts Exchange works, so I'll try to explain what happened in THIS question -- and looking at it a little more closely, I think that perhaps I did as the asker requested a little too quickly.

The asker asked his question, and then said "changed this to
gcc -o myprog -I/A/B/C/myInclude -L/A/B/C/mylib myprog.c -lmystatic -lpthread
and it worked". He then posted a question in CS asking that the question be deleted. I confess that I didn't see his request for an explanation, but I also felt that since he'd answered his question and posted how he had answered it, the question deserved to be saved. So I refunded his points and closed the question.

If I had been paying more attention, I would have questioned the asker about your comments, in light of his request for an explanation, and would have suggested that you did deserve something for your efforts. As such, there are points for you at https://www.experts-exchange.com/questions/20394289/For-fridom-re-20393213.html

Netminder
CS Moderator


OMG . u shud have told me earlier that .. u crave for points so badly .. tell me how many points to u need .. i'll create a question for u.. with max points and best grade ..
come on man learn something from opensource .. just points and money isnt every thing..
having a healthy and fruitful discussion is what i look for on this lovely site.. and not just points..
any way tell me how many points do u need ..
To get things rigth. I do post regular in diverse Newsgroups. And I probably would have answered the question even without points. But writing "u crave for points so badly" is rididioouls and I reject this placating. The question was answered correctly. So I want the points for it, is there anything unclear with that?

And telling me about open source is telling the wrong one. I do use OS regular but I too earn my living on offering our products. If you want to do us to do an OS project for you we  do not have any problem with it.

And it's nevertheless not the point here. You wanted to know why things worked that way and you said "the points" are yours. So give them to me.

Friedrich
To Netminder.

Well he does not have an explanation why one way works the other not and I explained it. That's it.

Regards
Friedrich
ok a bit of misunderstanding ..
actually.. when i said .. explain this and get points .. i waited for some time and before going back to home i requested for deletion of question .. and after u  answered the question .. Netminder had no idea abt ur answer when he was deleting that..
and nuthing much ...
and still i doubt the order of arguments cannot be resolved by the GCC command line only .. but logically u r correct .. no doubt abt that
and i also had similar explanation for me .. but i myself wasnt convinced..

anways.. thanks for ur comments .. if u still need points ... i wont mind...u can have as many as u wish
It can not be resolved on the command line. It's your responsibility as programmer to put the libraries in the correct order. Why do you think do thing like gtk-config and simular do exist?

Friedrich
aksyayxx,

You don't need to post a "points for" question. I've already done that.

fridom,

Please see https://www.experts-exchange.com/questions/20394289/For-fridom-re-20393213.html

Netminder
The problem seems not to be solve  for aksyayxx. I tried to tell him that there is not way of resolving the library order but with the appropriate tools and/or by the programmer.

Regards
Friedrich
ok got it fridom .. see i knew this .. that there has to be order of linking libraries .. like in some case  lpthread shud compe before some other link option ..

.. but i never thought abt this scenario ..
thanks again for ur comments ..  
but still i think that i can write one small wrapper over gcc .. which will take all '-l<library>' options and put it all in the end .. and pass it to real gcc .. thats what i wanted gcc to do for me .. ( though the order of libraries within linking options ..has to be decided by the user itself)
..
i hope u understand what i m trying to say..
....i m trying to group the linking and compiling options ..
the order within compling and linking option groups has to be predecided..

nuthing much
thanks again.. for now i m convinced abt the current way .. that u suggested.