Help linking libraries

Started by
10 comments, last by Rycross 14 years, 6 months ago
Hi. I want to learn a few APIs (Qt, WinAPI), but I'm clueless as far as how to link the libraries. i.e. how to use the headers associated with a given external library. Yes, I've looked online. I followed one tutorial on getting Qt to work with Visual C++, and I ended up spending 5 hours building the entire library by accident(lol). Even after that, it still wasn't clear as to what I was supposed to do to use the headers. Other tutorials have told me to create environmental variables - I've done that. So what's ended up happening is that every time I wanted to use an API, I just ended up downloading the specific IDE(WinAPI - Visual C++, Qt - Qt Creator), which is really inconvenient and pretty stupid. Can someone please show me how to do it via command line? I have multiple IDEs(Code::Blocks, MSVC++, etc), but I figure that the setup for every library I want to use isn't going to be the same if I ever switch. So I'll just compile them via command line. Plus, for the sake of learning, it's going to be much more simplistic.
Advertisement
What command line program? The issue here is that there isn't any one way to tell every compiler in existence to link in libraries. Visual C++ is going to be different than GCC, is going to be different that MingW. The best thing you can do is to understand linking conceptually, pick your compiler/s and then figure out how to specify libraries for linking for each one.
The basic gist is you just supply a path to the compiler to look for the headers, and you supply a path to the linker to look for libraries.

In Visual Studio you can find these in the project settings under C/C++ and Linker, respectively. Under C/C++ it's called Additional Include Directories, it's called something similar for the linker. Then you just add the path to the headers or libs, depending. Additionally, for libs you add the library name itself to the libs path (also under the Linker settings). Just poke around in the project properties and you should be able to figure it out.

APIs aren't like programs, you don't "install" them and they just work. Installation just throws some files into a folder. You then just need to point the compiler and linker to the correct folders and it'll to the rest.

So basically, I think that what you are doing is trying to solve the problem of "how do i get libraries to work" when you should be trying to solve the problem "how do I get <insert IDE here> to use libraries". i.e. look at the documentation for your specific IDE, not the documentation on the library.

-me

[Edited by - Palidine on October 15, 2009 7:02:13 PM]
Yes, I've gone to Visual C++'s Project Directories section before and added Qt bin and lib. It doesn't help.

http://img80.imageshack.us/img80/631/28836951.jpg

I'm using the MinGW compiler(not in the image).
Are you sure the headers aren't meant to have .h extensions?

See here. It looks suspiciously like the tutorial code in your screenshot :)
Actually, GCC and MinGW work basically the same, at least as far as command line usage.

Generally, what you have to do is both specify where the libraries are, by setting search directories, and which libraries to link.

With MinGW on the command line, search dirs for includes and libraries are indicated by the options -Ipath/to/includes and -Lpath/to/libs, respectively. Paths can use either forward or backslashes and be relative or absolute. Just be sure to quote ones with spaces in them. To link libraries with your program, you'll likely use -lname. This instructs the compiler to look in its search directories for a library file named libname.a and link it with your program.

In an IDE, it probably won't be that complicated. You'll just have to set the search directories, which you already seem to have done, and then tell each project which specific libraries it needs. In CodeBlocks this is in Build Options->Linker Settings. I tend to put -lname in the "Other options" area, but you can use the "Link libraries" tool. It's probably better if you learn to do it the way they intended. For any other IDEs, I don't know. It's been too long even since I used Dev-C++. [lol]

Hope that answers at least a few of your questions.
How am I supposed to know which libraries to link? Sounds silly, but the book I'm using just goes straight into introducing code examples (C++ GUI programming with qt4 second edition).

I'm actually able to compile with code::blocks, but it's giving me a popup error:

"The procedure entry point _ZN12QApplication4execEv could not be located in the dynamic link library QtGui4.dll"
Quote:Original post by infinite
How am I supposed to know which libraries to link? Sounds silly, but the book I'm using just goes straight into introducing code examples (C++ GUI programming with qt4 second edition).



You link the *.lib files that QT gives you. In Visual C++,you have to go into your project settings and set which libraries to link. Adding it to the directories just tells VC++ where to look for it, so you don't have to specify the full path.
Ok, this is really starting to frustrate me.

Code blocks has two different linker settings(I don't know what they're for). One only lets you select an individual library file, while the other lets you set a directory location.

Only AFTER I set the individual file "linker setting" was I able to compile(I guessed which library file it was). If I just set the directory, I get errors.

http://img96.imageshack.us/img96/1223/msvcqt.png
http://img194.imageshack.us/img194/8338/qtcb.png


In MSVC++, I set the Qt Directory for include and library files, and I still get this crap:


http://img194.imageshack.us/img194/278/msvc.png
Why did you put $(QTDIR)? Who told you to do that? Try setting the include and library directories to point to actual directories and not use an 'environment variable' that probably doesn't exist.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement