C++ Library Includes

Started by
10 comments, last by Grain 17 years, 8 months ago
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?
Woooooooot!
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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
How do you compile the project into a library? That seems like a viable option! :D

EDIT: Using VC++ Express 05, just so you know.
Woooooooot!
To create a static library you select the static library option when selecting the project type when you create a new project.
There's no option for that in Express. Is there a project setting? I can't seem to find it.

EDIT: Found it. Thanks. :)
Woooooooot!
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.
Woooooooot!
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.
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?
Woooooooot!
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?
Just including it in the project via the #pragma preprocessor directive.

#pragma comment(lib, "mylib.lib")
Woooooooot!

This topic is closed to new replies.

Advertisement