【问题标题】:Error LNK2019 when try to use function from namespace尝试使用命名空间中的函数时出现错误 LNK2019
【发布时间】:2012-10-04 10:55:41
【问题描述】:

我正在为我的项目编写一些实用程序函数。当我尝试使用嵌套命名空间中的一些函数时,出现了一个奇怪的 LNK2019 错误。我尝试在 google 和 stackoverflow 中进行搜索,但没有得到任何帮助。

我的文件

头文件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 文件X.cpp

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

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

错误信息

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

编辑(我的问题的解决方案)

我的文件X.{h,cpp} 在项目A 中,我在项目B 中使用这些函数。如果我在函数原型中使用__declspec(dllexport),我可以在项目B 中使用这些函数,因为它使用A 类似DLL。我在How to use functions from different C++ projects in Visual Studio 2010?(@Luchian Grigore 回复)和Visual studio: question about linking functions between two projects(@dascandy 回复)中得到了这个提示。

【问题讨论】:

  • 您是否将X.obj 链接到您将D.obj 链接到的任何二进制文件中?
  • 它们在我的 Visual Studio 解决方案中的不同项目中。在我的 D 属性页 > Common Properties > Framework and References 中,我引用了创建这些命名空间的项目。
  • 因为它们位于不同的项目中,您必须提供包含文件的完整路径,而不仅仅是简短的“X.h”。当您使用“某些文件名”时 - 编译器在当前项目的文件夹中查找给定文件
  • 当我将所有实现移至X.h 时,我可以编译。
  • 编译X.cpp的项目是静态库吗?您选择的“参考”类型是否暗示链接到参考项目的输出?

标签: c++ visual-c++ namespaces compiler-errors


【解决方案1】:

如果函数的代码位于不同的项目中,则必须将其编译为静态或动态库,然后在链接器 > 输入 > 附加依赖项下的项目选项中将库添加到调用项目(对于 MS Visual Studio) .

【讨论】:

    【解决方案2】:

    你需要做两件事:

    • 在 Linker > General > Additional Dependencies 下指定您正在使用的库的路径 + - - 在 Linker > Input > Additional Dependencies 下指定您正在使用的库

    【讨论】:

      猜你喜欢
      • 2014-12-03
      • 2021-07-06
      • 2021-01-23
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多