Visual C++ include directories help

Started by
11 comments, last by sheep19 15 years, 5 months ago
Hi. I made some headers I want to include in every project from the include directories. So I'll be able to do #include <hello.h> instead of "hello.h", and I won't need to place them in the same directory every time. So I did this: Image and video hosting by TinyPic Image and video hosting by TinyPic but it doesn't work. I don't a get any compile errors; I get link error:

#include <iostream>

#include <palindrome.h>

using namespace std;

int main()
{
	cout << palindrome(53);

	cin.get();
	return 0;
}


1>main.obj : error LNK2019: unresolved external symbol "int __cdecl palindrome(int)" (?palindrome@@YAHH@Z) referenced in function _main 1>C:\Users\User\Documents\Visual Studio 2008\Projects\Palindrome\Debug\Palindrome.exe : fatal error LNK1120: 1 unresolved externals [Edited by - sheep19 on November 16, 2008 12:44:40 PM]
Advertisement
Where is the definition for palindrome()? If it's in another .cpp file, did you add that file to the project?
Quote:Original post by Gage64
Where is the definition for palindrome()? If it's in another .cpp file, did you add that file to the project?


int palindrome(int num){	int the_palindrome = 0,		power = get_power(num),		temp = power;		for(int i = 1; i <= power; i *= 10, temp /= 10)		the_palindrome += num / i % 10 * temp;	return the_palindrome;}


I haven't added it to the project. Do I have to? I don't add <string> or <vector> to my projects, I want this to be like that. Is that possible?
Quote:Original post by sheep19
I haven't added it to the project. Do I have to?


Yes, otherwise the linker won't be able to find the definition.

Quote:I don't add <string> or <vector> to my projects, I want this to be like that. Is that possible?


As far as the standard library is concerned, I think there's some special setup where the appropriate libraries are linked without you having to specify them, but I don't know the details, and I'm not sure if it's possible to do something similar with custom libraries.

Also, a lot of those files contain template classes/functions and inline functions whose definitions are completely in the header files, so there's nothing to link.

Finally, I strongly recommend that you read this article.
One VS specific option is as follows:

Set up a custom Lib directory somewhere and point VS at the directory via the Library Files section of the VC Directories dialog.

Create a new empty static library project.

Add the .h file and the .cpp file of your library code to this project and compile.

Find the resultant .lib file and put it in the Lib directory you set up earlier.

Now, in your library .h file, add the line:

#pragma comment(lib,"name_of_the_lib.lib")


where name_of_the_lib is the name of your library file (obviously).

Now, in a fresh new project, you can just #include <name_of_the_lib.h> and you are good to go.

<string> and <vector> are all template code, so all the information the compiler needs is actually in those headers (or included in the headers via implementation files).

However, lots of the standard library is non-template and so is "magically" included by the compiler automatically linking against certain lib files in the background. The libraries linked by default are listed in an XML file somewhere deep in the bowels of your VS installation.
Actually it is pretty easy in the drop down that has include files there is an entry where you can tell it where to find libraries.
Quote:Original post by stonemetal
Actually it is pretty easy in the drop down that has include files there is an entry where you can tell it where to find libraries.


That tells VS which directories to search to find .lib files. To link against a specific .lib file, you have to specify the lib either with a #pragma, by adding the .lib to the project's additional dependencies or by adding the lib to the list of default libraries in the XML file somewhere in the VS installation.
Quote:Original post by EasilyConfused

Create a new empty static library project.


There is isn't such option. I'm using Visual C++ 2008 EE. Do you mean an empty project? (That's what I did)

Quote:
Add the .h file and the .cpp file of your library code to this project and compile.

Find the resultant .lib file and put it in the Lib directory you set up earlier.

How do I find it? it isn't in the folder of the project.


Quote:Original post by sheep19
There is isn't such option. I'm using Visual C++ 2008 EE. Do you mean an empty project? (That's what I did)


Just create either a console or Win32 app, then follow the wizard all the way through. On the last page, select Static Library and click the Empty Project option. Fully present in Express Edition.

Quote:Original post by sheep19
How do I find it? it isn't in the folder of the project.


If you have compiled in debug, it is in the Debug folder in your project top directory. If release, it's in Release directory.
I didn't do it correctly I think.

Image and video hosting by TinyPic
Image and video hosting by TinyPic
Image and video hosting by TinyPic
Image and video hosting by TinyPic

This topic is closed to new replies.

Advertisement