Sign in to follow this  
sheep19

#including custom headers in VC++ 08

Recommended Posts

I created a Sprite class to help me with my games. It uses SDL. I have some questions. To make a header available in this form #include <myheader.h> I have to create a lib file. I know that process. Now, I'm having some trouble doing this with my Sprite class. SDL uses dlls, so does this mean that I have to output a dll and not a lib file? Also, in the project that I will create the dll (or lib?), I have to include SDL.lib etc in linker-input right? Thanks in advance.

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
To make a header available in this form #include <myheader.h> I have to create a lib file. I know that process.
No you don't, you just have to add the directory that myheader.h lives in to your include path. It doesn't matter if it's a lib or not.

Quote:
Now, I'm having some trouble doing this with my Sprite class. SDL uses dlls, so does this mean that I have to output a dll and not a lib file? Also, in the project that I will create the dll (or lib?), I have to include SDL.lib etc in linker-input right?
You can create a static library (a .lib or .a) and dynamically link it to a dynamic library (a .dll or .dylib). So if you're using the SDL dll, but you're creating a static library, you can just link your static library to the SDL dll. You don't have to turn your static library into a dynamic library.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
You can create a static library (a .lib or .a) and dynamically link it to a dynamic library (a .dll or .dylib). So if you're using the SDL dll, but you're creating a static library, you can just link your static library to the SDL dll. You don't have to turn your static library into a dynamic library.


I meant being able to use it without including it in the header,like including <string> for example.

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
Quote:
Original post by MikeTacular
You can create a static library (a .lib or .a) and dynamically link it to a dynamic library (a .dll or .dylib). So if you're using the SDL dll, but you're creating a static library, you can just link your static library to the SDL dll. You don't have to turn your static library into a dynamic library.


I meant being able to use it without including it in the header,like including <string> for example.


You mean you don't want to include the SDL headers in your own header file because that would force any users of your header file to also include the SDL headers? The only way I know of is the pimpl idiom, but of course if you expose any SDL features in your interface you'll have to include the SDL headers, so the pimpl idiom can only help if you are totally hiding your usage of SDL.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
Quote:
Original post by sheep19
Quote:
Original post by MikeTacular
You can create a static library (a .lib or .a) and dynamically link it to a dynamic library (a .dll or .dylib). So if you're using the SDL dll, but you're creating a static library, you can just link your static library to the SDL dll. You don't have to turn your static library into a dynamic library.


I meant being able to use it without including it in the header,like including <string> for example.


You mean you don't want to include the SDL headers in your own header file because that would force any users of your header file to also include the SDL headers? The only way I know of is the pimpl idiom, but of course if you expose any SDL features in your interface you'll have to include the SDL headers, so the pimpl idiom can only help if you are totally hiding your usage of SDL.


No,sorry for not explaining.

The compiler has directories for headers, source files and library files. I want to be able to put my headers in those directories and just #include <myheader.h> (using <> not ""). So in order to do this I have to create a lib file. My question was, because SDL is a dynamic library (dll), if I have to create a dll as well (because my sprite header file includes SDL.h) and not lib file.

Share this post


Link to post
Share on other sites
Quote:

The compiler has directories for headers, source files and library files.

Mostly correct. It has a list of paths/directories that it searches for those files based on platform and such. It typically does not "search" for source files, not using the same mechanism.

Quote:

I want to be able to put my headers in those directories and just #include <myheader.h> (using <> not "").

This is generally extremely poor practice and potential dangerous. Better to put your header in some directory of your own choosing and add that directory to the compiler's search paths. That gives you the behavior you want (being able to use the angle-bracket form of #include) in the correct manner, without doing evil, dangerous things to the compiler's mostly-private directories.

Quote:

So in order to do this I have to create a lib file.

This is not true. You do not need to create a library file just to put a header in a common location. It's acceptable to want to, and often useful, but it is incorrect to assert that you must.

Quote:

My question was, because SDL is a dynamic library (dll), if I have to create a dll as well (because my sprite header file includes SDL.h) and not lib file.

You do not. As long as the appropriate symbols are available at link time, your program will link. This is also why you do not need to create a static library to place your header someplace else.

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
The compiler has directories for headers, source files and library files. I want to be able to put my headers in those directories and just #include <myheader.h> (using <> not ""). So in order to do this I have to create a lib file.

Using <> for your includes has nothing to do with whether or not you use a .lib file. To use <> for your includes, just put the directory that your header files are in on the include search path. In recent versions of MSVC you could do this by going to Tools->Options->Projects and Solutions->VC++ Directories and add the directory to the include path.

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
No,sorry for not explaining.

The compiler has directories for headers, source files and library files. I want to be able to put my headers in those directories and just #include <myheader.h> (using <> not ""). So in order to do this I have to create a lib file. My question was, because SDL is a dynamic library (dll), if I have to create a dll as well (because my sprite header file includes SDL.h) and not lib file.
That question right there, that very one you just asked, was answered in my first post. Like I said, you don't have to create a library to use <> instead of "", you just have to put the header's directory in you project/compiler include path (if you don't know how to do this, tell us what IDE or compiler you're using and we can tell you; SiCrane told how to do it for MSVS). And as I said, just because SDL is a .dll, doesn't mean (or require) that your project be a .dll as well. Your project can be a .lib and still use the SDL .dll.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
if you don't know how to do this, tell us what IDE or compiler you're using and we can tell you; SiCrane told how to do it for MSVS

Psst. <whisper>Thread title.</whisper>

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by MikeTacular
if you don't know how to do this, tell us what IDE or compiler you're using and we can tell you; SiCrane told how to do it for MSVS

Psst. <whisper>Thread title.</whisper>


Hehe... *facepalm*

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
okay, then do this:

-right click your project
-go to configuration properties->general
-set 'Application (.exe)', under 'configuration type' to 'static library (.lib)', compile, et voila... there is your static library

Do as I told one post above, and you can use any class/structure/function/variable you defined in the library.... hope that was enough information!

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
Yeah but I need to make the lib file first.
Then create a static library, not a console application - they're two completely different things.

Quote:
Original post by Evil Steve
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.

Share this post


Link to post
Share on other sites
I did that:


#include "SDL/SDL.h"
#include "sprite.h"

Sprite *s = new Sprite(0, 0, 0);

int main(int argc, char *args[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
return 1;

return 0;
}




(I know it's a memory leak)
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Sprite::Sprite(short,short,struct SDL_Surface *)" (??0Sprite@@QAE@FFPAUSDL_Surface@@@Z) referenced in function "void __cdecl `dynamic initializer for 's''(void)" (??__Es@@YAXXZ)

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
I did that:

*** Source Snippet Removed ***

1>Linking...
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Sprite::Sprite(short,short,struct SDL_Surface *)" (??0Sprite@@QAE@FFPAUSDL_Surface@@@Z) referenced in function "void __cdecl `dynamic initializer for 's''(void)" (??__Es@@YAXXZ)
You still have to link to the .lib file you created, it contains the code for your Sprite class.

Your static .lib project has no main() function, it's just code.
Your application .exe project has no library code (Sprite.cpp in this case), it links to the .lib file for your library, which contains that code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this