c++ - Visual Studio 2012 - 两个项目 -> 链接器错误 (C++)

标签 c++ visual-c++ linker visual-studio-2012 linker-errors

首先是我的简单设置: 我有 2 个 VS2012 项目。现在我想在项目 A 中使用项目 B 中的类。 我将项目 B 添加到 A 的项目依赖列表中,并在必要时导入了 header 。 (例如#include"..\src-pool\Coords.h";)。

到目前为止,非常好 - 没有编译器错误。 但是当我尝试构建项目时,出现了一些链接器错误:

Fehler  1   error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall Coords::Coords(double,double)" (??0Coords@@QAE@NN@Z)" in Funktion ""public: void __thiscall TileDownloader::calculateBounds(double *,int)const " (?calculateBounds@TileDownloader@@QBEXPANH@Z)".  C:\Users\username\documents\visual studio 2012\Projects\CPPHA\project\TileDownloader.obj    

对不起,这是德文版的 VS。 “Verweis auf nicht aufgelöstes externes Symbol”表示:链接到未解析的外部符号。

有什么想法吗? =)


完成这个(这是我要导出并在其他项目中使用的类)

坐标.h

#pragma once
#include <iostream>
#ifdef EXPORT_MYCLASS
#define MYCLASSEXPORT __declspec(dllexport)
#else
#define MYCLASSEXPORT __declspec(dllimport)
#endif


class  MYCLASSEXPORT  Coords
{
public:
    Coords(double lat, double lon);
    ~Coords(void);
    double getLon() const;
    void setLon(double val);
    double getLat() const;
    void setLat(double val);

    void printInfos() const;

private:
    double lat, lon;

};

但我收到警告“不一致的 dll 导出”和相同的错误。抱歉,我是 C++ 新手


我想像这样使用它

#include "..\src-pool\Coords.h"

class TileDownloader
{
public:
    TileDownloader(void);
    ~TileDownloader(void);


    void  calculateBounds(double* array, int zoomLevel) const;
    void  downloadTiles() const;

private:
    double maxLat, maxLon, minLat, minLon;

};

最佳答案

链接器需要做三件事才能找到类方法:

  1. 您引用了正确的 dll/项目
  2. 您在该 dll/项目中实现了该方法。
  3. 它通过 dll 导出暴露在外部 ( __declspec(dllexport) )

在 header 中声明导出的常见做法:

#ifdef EXPORT_MYCLASS
#define MYCLASSEXPORT __declspec(dllexport)
#else
#define MYCLASSEXPORT __declspec(dllimport)
#endif

class MyClass
{
     MYCLASSEXPORT MyClass();
}

然后您可以在导出 dll 的预处理器定义中定义该预处理器参数。

在 Visual Studio 中:
项目属性 -> 配置属性 -> C/C++ -> 预处理器 -> Prerpocessor deffinitios

关于c++ - Visual Studio 2012 - 两个项目 -> 链接器错误 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664521/

相关文章:

c++ - 对 `nfsInit` 的 undefined reference

c++ - 我在 C++ 初学者中遇到了无限循环问题

C++:响应 Windows 注销的清理操作

c++ - 运算符的多重重载

c++ - 我应该如何将 placement new 与自定义分配 API 一起使用?

c++ - IWebBrowser2 可以将 cookie 存储在用户指定的文件夹中吗?

visual-c++ - 未收到已注册的窗口消息 TaskbarButtonCreated

linker - C - 为什么 ANSI 仅指定 6 个字符作为外部标识符中有效字符的最小数量?

c++ - 我可以使用 MS 工具将浮点错误转变为 C++ 异常吗?

c++ - 使用 system() 从 C 程序调用 NASM 会生成不同的目标代码,然后使用 Bash 调用