1

I'm writing some utils functions to my project. I got a strange LNK2019 error when I tried to use some functions from nested namespace. I tried to search in google and in stackoverflow but I didn't get something to help me.

My files

Header file X.h:

#pragma once

namespace A {
    namespace B {
        /**
         * A função recebe edValue e devolve em ponto flutuante
         * o inteiro mais próximo de edValue.
         * 
         * Método usado: http://en.wikipedia.org/wiki/Directed_rounding#Round_half_up
         *
         * @param edValue valor que será arredondado.
         * @return o inteiro mais próximo à edValue (em ponto flutuante).
         */
        double round(double edValue);
    }
}

CPP file X.cpp

#include "StdAfx.h"
#include "X.h"
#include <cmath>

double A::B::round(double edValue)
{
    return floor(edValue + 0.5);
}

Error message

7>D.obj : error LNK2019: unresolved external symbol "double __cdecl A::B::round(double)" (?round@A@B@@YANN@Z) referenced in function "public: void __thiscall

Edit (solution to my problem)

My files X.{h,cpp} are in project A and Im using those functions in project B. If I use __declspec(dllexport) in my function's prototype I can use those functions in project B, cause it uses A like DLL. I got this tip in How to use functions from different C++ projects in Visual Studio 2010? (replied by @Luchian Grigore) and Visual studio: question about linking functions between two projects (replied by @dascandy).

Community
  • 1
  • 1
  • Are you linking `X.obj` into whatever binary that you're linking `D.obj` into? – CB Bailey Oct 04 '12 at 10:57
  • they are in differents projects in my visual studio solution. In my D property pages > Common Properties > Framework and References I have reference to project where I create those namespaces. – Wanderley Guimarães Oct 04 '12 at 11:00
  • Because they are in different projects you have to provide full path to included file, not just short "X.h". When you use "some file name" - compiler looks for given file in folder of current project – spin_eight Oct 04 '12 at 11:03
  • When I move all my implementation to `X.h` I can compile. – Wanderley Guimarães Oct 04 '12 at 11:11
  • Is the project that compiles `X.cpp` is a static library? Does the type of "reference" that you've selected imply linking against the output of the referent project? – CB Bailey Oct 04 '12 at 11:22

2 Answers2

0

If the code for the function is in a different project you must compile it to a static or dynamic library and then add the library to the calling project in the project options under Linker > Input > Additional Dependencies (for MS Visual Studio).

Kit Fisto
  • 4,385
  • 5
  • 26
  • 43
0

You need to do 2 things:

  • Specify the path of the library you are using under Linker > General > Additional Dependencies + - - Specify which library you are using under Linker > Input > Additional Dependencies
ISTB
  • 1,799
  • 3
  • 22
  • 31