Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Scipi on October 13, 2010, 11:27:35 pm

Title: A few C++ questions
Post by: Scipi on October 13, 2010, 11:27:35 pm
How do you add libraries to where you can just include the .h file? Im trying to link with allegoro for gui's and graphics and stuff.

And what exactly are pointers good for. I know what they do and what they are but what can they do that variables can't?

Thx.
Title: Re: A few C++ questions
Post by: Empyreal on October 13, 2010, 11:55:49 pm
I'm not sure I completely understand your first question, but if I do the answer will vary depending on which linker you're using.  If you're compiling and linking in one step with g++, it'll look something like:
g++ -o progname progname.cpp -lallegro
The form for that last argument is -lNAME_OF_LIBRARY.

Pointers store the address of a variable, structure, etc. The most straightforward usage of them is to give functions access to large amounts of data without the tremendous (in some cases) overhead of actually passing all of that data.
Title: Re: A few C++ questions
Post by: Scipi on October 15, 2010, 01:52:07 am
Ok think I see the use of pointers a little.

I was really talking about how I would go about adding a library so I could just include it from anywhere with #include.
Title: Re: A few C++ questions
Post by: Empyreal on October 15, 2010, 02:11:14 am
Pointers can be confusing at first, but once you use them a few times, you'll see they can be really useful.  In C++, the need to use pointers is not as great as it is in C thanks to classes.

Okay, I see.  #include isn't used to include libraries.  It is used to include header files.  For simplicity's sake, let's just say header files contain function prototypes. These prototypes are needed by the compiler to produce the object file.  All the compiler does is make sure you're passing the correct types of arguments and using the return value appropriately.  The linker is responsible for connecting these references to the actual functions.

So, simply put: you cannot include libraries at all.  You have to include the relevant header files and link the library.
Title: Re: A few C++ questions
Post by: Quigibo on October 15, 2010, 02:56:23 am
In visual studio, you can import a library from a header file like this:

Code: [Select]
#pragma comment (lib, "mylibrary.lib")
Title: Re: A few C++ questions
Post by: DJ Omnimaga on October 15, 2010, 03:28:10 am
Pointers can be confusing at first, but once you use them a few times, you'll see they can be really useful.
Yeah they took me quite a bit to understand in Axe Parser x.x
Title: Re: A few C++ questions
Post by: Scipi on October 15, 2010, 11:49:19 pm
Yeah I use MinGW as my compiler. Im still having problems with using allegro in my programs. I use Code::Blocks with MinGW. I think I might not have the dll file required. Though I haven't found any DL's.