#including custom headers in VC++ 08

Started by
24 comments, last by Evil Steve 15 years, 3 months ago
Do the header and source file need to be in the same path? Because I'm getting link errors when they are in different folders...
Advertisement
No.
I want to make a lib file.
This is what I do:

I create a new project:
Image and video hosting by TinyPic

I set it to be an empty windows console app.

Then I go to the project options and in the linker I add SDL.lib and SDLmain.lib.

In main I have this code:

#include "SDL/SDL.h"int main(int argc, char *args[]){	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)		return 1;	return 0;}


Now I need to add the header and source file that I want to make the lib for.
One question here: Do they have to be in the same location as the project? Or can I just include them while they are somewhere else? (I haven't created the lib yet)
Quote:Original post by sheep19
I want to make a lib file.
This is what I do:

I create a new project:

I set it to be an empty windows console app.

Then I go to the project options and in the linker I add SDL.lib and SDLmain.lib.

In main I have this code:

*** Source Snippet Removed ***

Now I need to add the header and source file that I want to make the lib for.
One question here: Do they have to be in the same location as the project? Or can I just include them while they are somewhere else? (I haven't created the lib yet)
If you want to create a .lib file, then you want to create a new Win32 project, and select "Static library" when it asks what type you want to create.

EDIT: Removed image from quote
But I have to supply an exe that uses that header and source file right? That's what I'm trying to do.
Quote:Original post by sheep19
But I have to supply an exe that uses that header and source file right? That's what I'm trying to do.
A static library is just compiled source code, it doesn't run. If you want to use your library, then yes - you'd create a console application and link with your created .lib file, and not your source files you used to create the .lib.

Just consider a .lib file as all of the .cpp files you used to create the lib.
That's not the problem. It is before I create the lib file. The .h and .cpp and placed eg in the desktop. Then I create a console app to link it with the .h and .cpp file. Do these(.h, .cpp and project) need to be in the same directory?
Quote:Original post by sheep19
That's not the problem. It is before I create the lib file. The .h and .cpp and placed eg in the desktop. Then I create a console app to link it with the .h and .cpp file. Do these(.h, .cpp and project) need to be in the same directory?
The .h file needs to be in the correct place relative to the .cpp file (I.e. if you include "header.h", then the header must be in the same directory as the .cpp file).
I'm not sure about the restrictions on where the .cpp file needs to be, relative to the project, I would have assumed Visual Studio would let you have .cpp files from anywhere. However, why not just put the .cpp in the same directory as the .vcproj and .sln files? And what is the exact error you get?
No you don't have to do that. The easiest, and best way to do this is as following:

In the project where you want to include the library:
- right click your project
- click properties
- unfold C/C++ and click on 'general'
- at the 'additional include directories', type (or browse to) the location of your headers

Now to include the library itself (the way I do it)
- unfold linker and click on 'general'
- at the 'additional library directories', type (or browse to) the location of your lib

Remember that MSVC++ has configurations. I think you're currently in the debug configuration. So all these directories have ONLY been added to your debug-configuration. So switch to 'release'-configuration, and do the same thing. The nice thing about this is, that you can compile your library to a debug-library and a release-library.
So in debug-configuration, point the 'additional library directories' to the folder with the debug-library. And in the release-configuration, you should point the 'additional library directories' to the folder with the release library...

The last thing you'll have to do is actually add the library to your project. You can do this under Linker->Input, but I always place the following code in a source code file:

#pragma comment(lib, LibraryName.lib) //for a LIB file#pragma comment(dll, LibraryName.dll) //for a DLL file offcourse


I hope this helps you out/
Yeah but I need to make the lib file first.

This topic is closed to new replies.

Advertisement