Jump to content
  • Advertisement
Sign in to follow this  
SouthernMunk

C++ Library Includes

This topic is 4382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've just finished (hopefully) my wrapper for the Windows API in C++ (window classes, windows, menus, networking and threading), and I was just trying it in a completely separate project. However, I'm getting linking errors because the main include file includes other header files and not the .cpp files, so the compiler gets all the class/function prototypes but not their definitions. Should I simply include "file.cpp" instead of "file.h" in the main library header, or is there a better way?

Share this post


Link to post
Share on other sites
Advertisement
You have a couple options: you can compile your code into a library or, if using an IDE, add its source files to the project. If using a command-line compiler, you'll have to compile those cpp files and link them with the rest when producing the final executable (which is what an IDE does for you).

Share this post


Link to post
Share on other sites
To create a static library you select the static library option when selecting the project type when you create a new project.

Share this post


Link to post
Share on other sites
OK, I've added a class to my .lib file and now I'm getting link errors. Could someone point me in the right direction to figure this out?

EDIT: MSVC++ Express 05 - the error is LNK2019 unresolved external symbol. I don't get it as I can't see what I'm doing different.

Share this post


Link to post
Share on other sites
Few questions for clarity:

What is the unresolved symbol? Is it one of the member functions of the new class you have added? Is the linker error when you compile the lib or when you compile the project that uses the lib? How are you linking the lib and .h file into the project that uses the lib?

I'm working on a .lib at the moment and in case this is of use (I appreciate my solution may be considered a bit overblown), I've done the following:

I have a C:\projects\library\include and ...\lib directories that I have added to VSExpress's include directories.

In the project settings for the .lib project, I copy-pasted the .h file from the project's folder into my ...\include folder, removed the orginal .h from the project then added in the copy in the ..\include folder so it pairs up with the original .cpp in the project folder.

I then used Project Properties->General to set the .lib project output to the ...\lib directory I set up.

With the test project that uses the lib, I now just

#include <mylib.h>

(which contains a #pragma comment(lib,"mylib.lib")

and use the lib. I can now just switch between the .lib project and the test project and, as long as I do a rebuild of both when I make changes to the .lib project, everything seems to work fine.

As I say, I appreciate this solution may not be to everyone's taste but it seems to be to be a reasonable way of developing a lib project and a test application project in tandem without too much messing about.

Share this post


Link to post
Share on other sites
It happens when I compile the project (not the lib). It is an unresolved external symbol that is a member of the new class.

Also, the class wraps up a bunch of time functions. However, Whidbey's autocomplete feature lists a "GetTickCount" member of the class, when there is no such thing? WTF?

Share this post


Link to post
Share on other sites
How are you including the .lib file in the project? If you are adding it through the Project->Linker->Inputs, are you sure it is pointing at the most up-to-date version of the .lib?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!