Advertisement

newbie question about g++'s library searching

Started by June 29, 2007 09:19 AM
2 comments, last by Khaos Dragon 17 years, 7 months ago
I am wondering if there is a way to tell g++ to recursively search for a library. For instance, let's say I am compiling a simple xlib program. The following works on most unix/linux setups. g++ simpxclient.cpp -L/usr/X11R6/lib -lX11 -o simpxclient What I would like to be able to do is something like g++ simpxclient.cpp -RecursiveSearch /usr/ -lX11 -o simpxclient This would be useful if I know I have the xlib library, but just want to compile a quick test program and not have to search for the library path. Alternatively I could do this even though it would be unrealistic, but it would be nice to know I have the option g++ simpxclient.cpp -RecursiveSearch / -lX11 -o simpxclient
You could always try to use find or something.

something like g++ -L`dirname (`find /usr -name X11`)` {files}

Not actually sure about the syntax for recursive ` marks, though. Basically what that does is run the command find /usr -name X11 and then toss its output to dirname (which trims off the filename and just gives you the directory) and then inserts it into the rest of the command.

You probably have to screw around with find to make it only return the first result it finds, I can't remember how that works.
Advertisement
Note that there is likely a maximum possible size of the command line for g++ (or any command) in your shell. Thus, find may be dangerous. Even worse, "xargs" (used to get around this limitation) won't work in this case.

A better way would be to use find (or, perhaps, locate/slocate, if installed and updated) to find the library in question. If you want library -lX11, do "locate libX11." to find all instances of the library, and see where they live. Then put that into your make file. You can even write a wrapper script that does the same thing for each -l option you pass (but then you're treading close to libtool, which is an approach I dislike).
enum Bool { True, False, FileNotFound };
Thanks for the help guys, that was all the info I needed.

This topic is closed to new replies.

Advertisement