-2

I have read this informative stackoverflow question regarding unresolved external symbols, but I am still not sure how to solve my issue.

Within Visual Studio 2012, I have a solution consisting of multiple projects, one of which is a static library called common. Each project that produces an executable consists of a header and associated cpp file of all global functions used throughout that specific program, called programglobals. In the process of developing one of these projects, I started duplicating some of this code from one project's programglobals to another. Now that I have completed all the projects, I want to extract the duplicate code into a associated header and cpp file within the common library, but I believe I might be referencing them incorrectly, which is producing these unresolved external symbol errors

Here is a dumbed down example of what I am currently attempting.

Common Library Files

//solutionglobals.h
void commonFunction();

//solutionglobals.cpp
void commonFunction() {
    int asdf;
}

Project A Files

// programglobals.h
#include "../common/solutionglobals.h

void functionUsedInProjectA();

// programglobals.cpp
void functionUsedInProjectA() {
    int x;
}

// output.h 
#include "programglobals.h"
void asdfA();

// output.cpp
void asdfA() {
    int x;
    functionUsedInProjectA();
    commonFunction();
}

Project B Files

// programglobals.h
#include "../common/solutionglobals.h

void functionUsedInProjectB();

// programglobals.cpp
void functionUsedInProjectB() {
    int x;
}

// output.h 
#include "programglobals.h"
void asdfB();

// output.cpp
void asdfB() {
    int x;
    functionUsedInProjectB();
    commonFunction();
}

Any reference to commonFunction() results in an unresolved external symbol error.

Thanks!

Community
  • 1
  • 1
halexh
  • 3,021
  • 3
  • 19
  • 19
  • Do you mean `commonFunction()` ? – Oncaphillis Nov 25 '14 at 15:10
  • For a question like that, it is necessary to show exact compilation commands and exact compilation error. You are just compiling/linking it wrong, thus the problem. – Freddie Chopin Nov 25 '14 at 15:13
  • You will have to specify in your executable projects that they reference the static lib. May be http://msdn.microsoft.com/en-us/library/vstudio/ms235627%28v=vs.110%29.aspx#uselibinapp helps (lower third of the article). – Oncaphillis Nov 25 '14 at 15:18
  • @Oncaphillis That was the solution. I was not adding it as a reference. Feel free to post that as an answer and I will select it as the solution. Thanks for your help - I am still getting used to Visual Studio. – halexh Nov 25 '14 at 15:31

2 Answers2

3

You will have to specify in your executable projects that they reference the static lib. May be http://msdn.microsoft.com/en-us/library/vstudio/ms235627%28v=vs.110%29.aspx#uselibinapp helps (lower third of the article).

Before you can use the math routines in the static library, you must reference it. To do this, open the shortcut menu for the MyExecRefsLib project in Solution Explorer, and then choose References. In the MyExecRefsLib Property Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the Add New Reference button.

Oncaphillis
  • 1,888
  • 13
  • 15
  • Might be nitpicking, but it might be an idea to add *how* to reference the static library. At the moment your answer *sort of* borders as link-only. – AStopher Nov 25 '14 at 15:39
0

The linker cannot 'see' your function and as such thinks that it does not have an external symbol referencing it, hence the error.

You must #pragma comment(lib, [library here]) to reference the external function.

The following code can be used to reproduce this error:

[header file- test.h]:

#include "StdAfx.h"

void someOtherFunction();
void someFunction(string thisVar);

[code file- test.cpp]:

#include "StdAfx.h"
#include "test.h"

void someOtherFunction()
{
    printf("Hello World!");
}

[function body for someFunction(string thisVar) is missing!]

AStopher
  • 4,207
  • 11
  • 50
  • 75