".h" Explanation and Downloading help

Would somebody please explain to me where to put the .h and .lib files I downloaded from this tutorial?


"To do this, we require the following files:

Console.h — for console output with color and explicit text positioning
Console.lib — the compiled console library, just add it to the project
conio.h — for reading input from the keyboard"

When you click a link at the bottom of a page, it downloads 2 files, one called "Console.h" and another named "ConsoleLib.lib". I am using MinGW, by the way.
Are you using an IDE or compiling from a shell?
An IDE, netbeans to be exact.
I don't know how netbeans works, sorry. As a general guideline you should put the header in a directory that your compiler searches for header files and the lib in a directory that your linker searches for libraries (and also link that library to the executable)

Code::Blocks lets you add custom search directories to compilers or on a project basis, you can try your luck in the settings window and look for an equivalent option
Last edited on
could you explain how to do it in code::blocks then? I use both.
First of all a disclaimer: it has been 2 years since I last used a shell and I was never good at it, so there might be errors

When you write something like #include <header> you are telling the compiler that you want to include a file that is found in the compiler's default search path for headers

When you're writing a program you will often make your own headers (or download some), and it's not good to put them in the default search path
So you usually put them in the same directory of the .cpp file. When you want to include such headers you write #include "header.h" The quotes tell the compiler that it should also search for the file in the same directory of the .cpp file
Or you can create a subfolder for headers called "folder" and write #include "folder/header.h". Basically, double quotes means "search for the header also in a directory starting from where the .cpp file is"
But what if your headers where in a very different place? Say they are located in D:\folder\asd\cpp\test\headers. You'd have to write that every time or else the compiler can't find your header and put it into the source, and it's really not smart as a solution. So when you compile a program you can tell the compiler that there are additional search directories in which he has to search for files.
You do that by invoking gcc with a -I argument, something like
g++ -I D:\folder\asd\cpp\test\headers file.cpp -o file

This allows you to write in the in file.cpp
#include "header.h" and still make it work even if your header is in that far away place

Now, for libraries it's pretty much the same. You have .lib files (though IIRC MinGW libraries are .a) and your compiler needs to know where they are to link them to the object files
The procedure above can be repeated for libraries, but the switch to use is -L insted of -I
There is one additional step though. For header, all you need to do is writing #include in the source file. Libraries need to be linked when you're compiling a program. You do that with the -l switch (lowercase L) and specifing the name of the library without the extension
Using the example from before it would look something like this
g++ -L D:\folder\asd\cpp\test\libs file.cpp -lConsoleLib -o file

So, let's say you downloaded those two files. You have a source file in a folder located in D:\cpp and two subfolders (headers and libs). It looks something like this


To compile this you would invoke
g++ -L D:\cpp\libs -I D:\cpp\headers file.cpp -lConsoleLib -o file

By the way, the default search path of MinGW is MinGW\include for headers and MinGW\lib for libraries

And now the reason why all of this is useless to you: IDEs take care of invoking the compiler using arguments specified in their settings. So you have to find the place where you can tell netbeans that he has to add the directories where you put the files to its search directories and also find the place where to tell him that he has to link a library to the .cpp file

I hope this is at least a bit useful. It took me quite some time to figure out all of this by myself with the help of google, hopefully you can have it easier
In C::B you can do it globally or on a project basis. Did you create a project or just a single file?
Last edited on
a project.
In the menu bar click on "project" and then "build options"
Go to "search directories". In "compiler" add the path to the folder of your headers and in "linker" the path the the libraries folder
Beware that on the left you can choose the build target for which these options will apply. To apply them to every target select the project name

To link a library go to "linker settings" and in "other linker options" write -llibname (eg -lConsoleLib)
I figured out how to connect the header and library on both code blocks and net beans, but it still isn't working; I'm getting the same error I did before. I'm starting to believe my compiler or this file is missing something crucial for the operation. Did you attempt to use the library yourself?
I tried it now. I can't get it to work too
I tried putting the option in both "Link libraries" with full and relative path and "Other linker options"
If I write the relative path in "Link libraries" it gives me a "cannot find -lConsoleLib.lib" error, in every other case it gives me a bunch of undefined references errors
I'm starting to think there's something wrong with the library...

It must be noted that standard GCC libraries are called libName.a (eg libConsoleLib.a), but .lib files can be used as well under some conditions (which I don't know, but I link against two SDL .lib so it IS possible)...

Sorry, I can't be of any more help
No don't be, you saved me a lot of time and I understand libraries now. Problem solved--the library doesn't work.
Topic archived. No new replies allowed.