#include files from another project in Visual Studio Solution

I am attempting to use code from another solution in a different solution. I am trying to include the header file from a different solution, in my open solution. The files compile and run fine if they are all in the same solution, but it would probably be beneficial to know how to include files without copy and pasting each one into the new solution.

I've searched numerous times on how to fix my error and have kept trying various solutions, one of which is:
http://stackoverflow.com/questions/601268/visual-c-include-files-from-other-projects-in-the-same-solution

Error message:
1
2
3
4
5
6
1
1>  circleType.cpp
1>circleType.obj : error LNK2019: unresolved external symbol "public: void __thiscall pointType::set_point(double,double)" (?set_point@pointType@@QAEXNN@Z) referenced in function "public: __thiscall circleType::circleType(double,double,double)" (??0circleType@@QAE@NNN@Z)
1>circleType.obj : error LNK2019: unresolved external symbol "public: void __thiscall pointType::print(void)const " (?print@pointType@@QBEXXZ) referenced in function "public: void __thiscall circleType::print_center(void)const " (?print_center@circleType@@QBEXXZ)
1>circleType.obj : error LNK2019: unresolved external symbol "public: __thiscall pointType::pointType(double,double)" (??0pointType@@QAE@NN@Z) referenced in function "public: __thiscall circleType::circleType(double,double,double)" (??0circleType@@QAE@NNN@Z)
1>C:\Users\user\Documents\Visual Studio 2013\Projects\ch13_p4_circletype\Debug\ch13_p4_circletype.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Right now I have tried doing the following:
With circleType solution open, right click on the file in the property manager window -> Configuration Properties -> C/C++ -> General -> Additional Include Directories, then putting
C:\Users\user\Documents\Visual Studio 2013\Projects\ch13_p3_pointType\ch13_p3_pointType;
in the Additional Include Directories.
I have tried it with a \ at the end, surrounding it with "" marks.

My solution is does not include any library files, (I have tried doing the solutions for libraries too in the SO link, but it also results in errors, this post is without library inclusions)

The file directory of the pointType header file and cpp file is:
C:\Users\user\Documents\Visual Studio 2013\Projects\ch13_p3_pointType\ch13_p3_pointType

I don't think that there is anything wrong specifically in my code as if I create a new solution with both pointType and circleType files in it, it runs fine without any errors.

If anyone can help point me in the right direction I'd greatly appreciate it!


Below is the pointType header file, pointType cpp file, and main file for pointType solution, and the circleType header file, circleType cpp file, and main cpp for the circleType solution.

pointType headerfile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef H_pointType
#define H_pointType

class pointType
{
public:
	void set_x(double x);
	//Post condition: x_cord = x;

	void set_y(double y);
	//Post condition: y_cord = y;

	void set_point(double x, double y);
	//Post condition: x_cord = x, y_cord = y;

	double return_x() const;
	//returns x_cord

	double return_y() const;
	//returns y_cord

	void print() const;
	//Prints out coordinates in format (X,Y).

	pointType(double x = 0, double y = 0);
	//Constructor. Sets x_cord = x, y_cord = y. If no parameters given sets x and y to 0.


private:
	double x_cord; //x-coordinate
	double y_cord; //y-coordinate
};

#endif 


pointType cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "pointType.h"
#include <iostream>

using namespace std;

void pointType::set_x(double x)
{
	x_cord = x;
}

void pointType::set_y(double y)
{
	y_cord = y;
}

void pointType::set_point(double x, double y)
{
	x_cord = x;
	y_cord = y;
}

double pointType::return_x() const
{
	return x_cord;
}

double pointType::return_y() const
{
	return y_cord;
}

void pointType::print() const
{
	cout << "(" << x_cord << "," << y_cord << ")";
}

pointType::pointType(double x, double y)
{
	x_cord = x;
	y_cord = y;
}


main cpp file in pointType
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "pointType.h"
#include <iostream>
using namespace std;

int main()
{
	pointType point1(1, 1);

	point1.print();
	cout << endl;

	point1.set_point(3, 3);
	point1.print();
	cout << endl;

	pointType point2;
	point2.print();
	cout << endl;

	cout << "Point1 return_x = " << point1.return_x() << " Point 1 return_y = " << point1.return_y() << endl;

	return 0;
}


circleType header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef H_circleType
#define H_circleType

#include "pointType.h"


const double pi = 3.1416;

class circleType : public pointType
{
public:

	void set_radius(double r);
	// radius = r. r must be >= 0

	void print_radius() const;
	// prints radius.

	double return_radius() const;
	// returns radius

	double calculate_area() const;
	//Returns pi*radius^2.

	double calculate_circumference() const;
	//returns 2* pi * radius

	void print_center() const;
	//prints coordinates of center.

	void set_center(double x, double y);
	//changes coordinates of center calling pointType.

	circleType(double x, double y, double r);
	//Constructor. radius = r, center x_cord = x, center y_cord = y.

private:
	pointType center;
	double radius;
};

#endif 




circleType cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "circleType.h"
#include <iostream>

using namespace std;


void circleType::set_radius(double r)
{
	if (r >= 0)
	{
		radius = r;

	}
	else
	{
		radius = 0;
	}
}

void circleType::print_radius() const
{
	cout << "Radius: " << radius;
}

double circleType::return_radius() const
{
	return radius;
}


double circleType::calculate_area() const
{
	double area = pi * radius * radius;

	return area;
}


double circleType::calculate_circumference() const
{
	double circumference = 2 * pi * radius;

	return circumference;
}

void circleType::print_center() const
{
	print();
}


void circleType::set_center(double x, double y)
{
	set_point(x, y);
}


circleType::circleType(double x, double y, double r)
{
	radius = r;
	center.set_point(x, y);
}


main cpp file in circleType solution.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include "circleType.h"
#include <iostream>

using namespace std;

int main()
{
	circleType cir1(1, 1, 5);
	cir1.print_center();
	cout << endl;

	cir1.print_radius();
	cout << endl;

	cir1.set_radius(100);
	cir1.print_radius();
	cout << endl;

	cir1.set_center(40, 3);
	cir1.print_center();
	cout << endl;


	return 0;
}
Your code is compiling fine. What you're getting is a linker error.
The linker can not find the implementation of your pointType class.

You have a couple of ways to solve this.
1) Add the pointtype.cpp source file to your project. This will cause the pointtype.cpp file to compiled as part of of your current project. The object file for the pointtype class will be in your current object directory in effect giving you two object files for the same class (one in the other project and one in this project).

2) Add the pointtype.o object file from the other project to the additional objects property item in your current project.
Hi AbstractionAnon,

Thanks for taking the time to reply to my question.

In response to your first solution:
I know that I can add the entire other file to my current project, that is hackish and will not benefit me in the long run when I am working with numerous files. For the problem I was working on, that is what I did, but I am asking this question for my general knowledge of how to use files from other solutions.

I tried adding the path of pointType.obj to the "Additional includes directory" but I have the same error. I do not know what you mean by "additional objects property item", I have looked and do not know where that is.

The file path to the pointType.obj is
 
C:\Users\user\Documents\Visual Studio 2013\Projects\ch13_p3_pointType\ch13_p3_pointType\Debug\pointType.obj


The file path to the cpp and header file of pointType is
C:\Users\user\Documents\Visual Studio 2013\Projects\ch13_p3_pointType\ch13_p3_pointType\


What I am looking for is if you, or anyone else, can tell me exactly what I need to type, in which part of the properties settings?
The purpose of my question is to be able to use files from another solution without adding the entire other solution to my current solution.

I must be doing something wrong, because the advice given here
http://stackoverflow.com/questions/601268/visual-c-include-files-from-other-projects-in-the-same-solution
worked for other people, but is not working for me.
If you're really set on learning how to do this once you have lots of code, you should distinguish between Solutions and Projects in the Visual Studio nomenclature.

A Solution is a collection of Projects. A Solution dictates things like build order and configuration among those Projects.

A Project dictates which code files are compiled into objects and how those objects are combined with other objects and libraries. A Project produces a library (static[.lib] or dynamic[.dll]) or executable.

If I were you, I'd have 5 Projects.
CircleLib : Contains CircleType.h and CircleType.cpp and builds into a static library.
CircleDriver : Contains main.cpp for the Circle stuff and a dependency on the static library that's built by CircleLib
PointLib : Contains PointType.h and PointType.cpp and builds into a static library.
PointDriver : Contains main.cpp for the Point stuff and a dependency on the static library that's built by PointLib
NewProj : Whatever you're trying to do now. Will contain a main.cpp of its own and dependencies on static libraries produced by PointLib and CircleLib.

There would be 3 solutions.
CircleSolution : Contains the CircleLib and CircleDriver Projects.
PointSolution : Contains the PointLib and PointDriver Projects.
NewSolution : Contains NewProj Project. This may also contain CircleLib and PointLib projects, which is useful if you need to change the CircleLib and PointLib over the course of developing NewProj.

Now we're talking about some serious project management overhead, but what you get is that once you've fully developed CircleLib and PointLib you only need to point to their headers and static libs in the "Additional Dependencies" of your new project (whatever the new project may be).

Does this sound like overkill for what you want to do? Then include the .cpp file in your new project.
Last edited on
Hi booradley60,

I appreciate your thorough response, and now have a better understanding of how to use the Visual Studio nomenclature.

I am used to building things like:
CircleSolution: Contains Circle.H, Circle.cpp

None of my classes or books so far have talked about incorporating a static library and drivers. For all of my projects for classes and book problems so far, they all use the format:
CircleSolution: Contains Circle.H, Circle.cpp, main.cpp
Or XSolution: X.H, x.cpp, y.h, y.cpp, ....etc.... main.cpp

Do you know of a way using the format that I am used to,
PointSolution: Contains point.H, point.cpp
CircleSolution: Contains Circle.H, Circle.cpp

and be able to use point solution within circle solution? (Circle class inherits from Point class).

According to this link
 
http://stackoverflow.com/questions/601268/visual-c-include-files-from-other-projects-in-the-same-solution 
(and many similar to it), people are performing exactly what I want to be able to do, with just adding the path of the desired .h and .cpp files to Additional Include Directories. They say you can add library path to the linker IF you are using static libaries.

I assume that means you are able to perform this without using libraries, and just editing the path in Additional Include Directories to be able to include header files located in another solution?

Or am I mistaken, and in order to do what I want to do, I must implement the entire full solution that you have outlined?

I am not adverse to learning how to implement what you are suggesting, but if I can do it in a much similar manner I would find that preferable. (As I have no idea how to do the library/driver implementations).
Here's something you could try. Instead of adding the .h file to your new project:
1. Go to Project -> Properties -> Configuration Properties -> C/C++ -> General
2. Click on "Additional Include Directories"
3. Click the drop-down arrow
4. Click <Edit...>. This should bring up a little box where you can add paths to your .h files.
5. Put in a path to the directory containing circleType.h, then put in the path to the directory containing pointType.h.
This will allow you to say #include "circleType.h" or #include "pointType.h" without actually having to add those .h files to your project.

Ok. Good. Now your new main inside of your new project can "see" all those declarations in circleType and pointType if you #include them. Now, we have to somehow get to the code that exists behind the interface given in the .h files.

This is the part I'm not sure about, but I'm guessing you can feed .obj files into the linker to get to the actual object code you've already compiled.

1. In your new project, go to Project -> Properties -> Configuration Properties -> Linker -> General
2. Click on Additional Library Directories
3. Click on the arrow dropdown
4. Just like you did with the header files, add paths to the directories that contain your .obj files.

Once you've done that, you've told Visual Studio where to look for library files. Now you tell it the names of the actual libraries or object files:

1. Project -> Properties -> Configuration Properties -> Linker -> Input
2. Click on "Additional Dependencies"
3. Click the dropdown arrow
4. Click <Edit...>
5. Type the name of the .obj files. You should see some .lib files already in there that are preconfigured for you. Just follow their lead except type in the name of your .obj files.

I'm not sure if this will work, since I've only done it with .lib files, but that's about the only suggestion I have for adding object code at the linking phase without a full library.
Last edited on
I know that I can add the entire other file to my current project, that is hackish and will not benefit me in the long run

I wasn't really recommending the first solution, but mentioned it for the sake of completeness. Not sure how much effort you want to go to to resolve this issue.

I tried adding the path of pointType.obj to the "Additional includes directory"

That won't accomplish anything since the compiler only looks for .h files in the "additional includes" directory.

What I am looking for is if you, or anyone else, can tell me exactly what I need to type, in which part of the properties settings?
booradley60 gave you better instructions for this than I did.

I generally create one solution for each complete executable program. That solution contains a project by the same name as the solution which contains files unique to that program. I then have a project under that solution for each library or DLL that the solution uses. These library/DLL projects get included in any solutions that use them. By marking project dependencies correctly, any changes to library files will cause those projects to get rebuilt in the correct order. It's not necessary to create a solution for library projects, but it is certainly something you can do if you want to create/release libraries independently from the solution for your executable. I most commonly do this with DLLs.

I would not create separate VS solutions for each source file (circle solution, point solution, etc). This will unwieldly very quickly.

Static libraries are a good way to built up collections of code. You can add many object files to a library. This linker will bring in only those routines from the library that you actually reference in your executable.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.