Reputation: 611
I am just beginning to learn C++ and using Visual Studio 2017 as the IDE. I want to stay away from the CLR, and even Windows specific code as much as possible. Every new project option seems to say 'Windows', and thus suggest Windows OS dependencies, except for General > Empty Project, so that is the option I have chosen.
I am trying to write a static library with some functions. Here's a simple example of what I'm trying to do.
//Lib1.h (Project A)
#pragma once
#ifndef LIB_H
namespace Lib1
{
double Add(double, double);
}
//Lib1.cpp (Project A)
#include "Lib1.h"
namespace Lib1
{
double Add(double a, double b)
{
return a + b;
}
}
//Main.cpp (Project B)
#include "Lib1.h"
using namespace Lib1;
int main()
{
double c = Add(1, 2);
}
Project A compiles without warning/error. I changed the Project Properties > General > Configuration Type to be Static Library (.lib), but I cannot actually find the compiled .lib file in the project directory, just an .obj file.
When I try and build Project B it throws the following error message.
"LNK2019 unresolved external symbol "double __cdecl FrameTransformations::Add(double,double)" (?Add@FrameTransformations@@YANNN@Z) referenced in function _main FrameTransformationTests S:\Projects\Cpp\Code\FrameTransformations\FrameTransformationTests\Main.obj 1"
I've had a look and it seems that project B can't find the Add function from project A, although I get auto-completion for the function and arguments through Intellisense (is this just because it can find the Lib1.h file ok?).
I have tried adding in the path to the Project A folder in the Project B Properties > C/C++ > General > Additional #using Directories (both to the Project folder and to the Debug folder where I expected the Lib1.lib file to be generated), but that hasn't worked.
Can someone please point me to what I've done wrong, this has had me stumped for a couple of hours now.
Thank you.
Upvotes: 1
Views: 3290
Reputation: 336
You need to add reference of Project A into Project B->Project Properties as below,
Also, you need to add a reference in like below
Upvotes: 1
Reputation: 15172
The .lib file is generally under the solution directory rather than the more specific project directory: Look in \ or possibly \\.
Upvotes: 3
Reputation: 1876
When you #include
a .h file, you are usually just including declarations into your code (exceptions apply, but not in this case). These are separate from definitions which are placed into .cpp files.
By default your projects do not automatically shared any of this information with each other. Your definitions nor declarations from Project A do not magically propagate to Project B, for example.
But as you've done here, you can reference declarations from Project A into Project B by #include
ing header files. These, however, are just declarations! They just explain the existence of a function but don't implement it -- You need to add the definition of your Add
function to Project B.
The process of bringing such definitions together is called linking. You need to link the definitions of Project A (the .lib, more precisely) into Project B. The way you do this changes with each version of Visual Studio just slightly, but it's usually under some "Linker Options" and some "Dependencies" tab/window.
Upvotes: 2