【问题标题】:VisualStudio 2012 - two projects -> linker error (C++)Visual Studio 2012 - 两个项目 -> 链接器错误(C++)
【发布时间】:2013-02-02 17:33:00
【问题描述】:

首先我的简单设置: 我有 2 个 VS2012 项目。现在我想在项目 A 中使用项目 B 中的类。 我将项目 B 添加到 A 的项目依赖项列表中,并在必要时导入了标题。 (例如#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;

};

【问题讨论】:

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


    【解决方案1】:

    链接器需要完成 3 件事才能找到类方法:

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

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

    #ifdef EXPORT_MYCLASS
    #define MYCLASSEXPORT __declspec(dllexport)
    #else
    #define MYCLASSEXPORT __declspec(dllimport)
    #endif
    
    class MyClass
    {
         MYCLASSEXPORT MyClass();
    }
    

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

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

    【讨论】:

    • 感谢您的回答,不幸的是它不起作用:/(见上文)
    • @area404,您是否在项目的预处理器定义(导出 dll)中定义了 EXPORT_MYCLASS?
    • @area404 我已经编辑了帖子来解释如何定义参数
    • 是的,我定义了它,但它不起作用。好像我在其他地方犯了错误。方法实现了,项目引用了..hm
    • @area404 该警告意味着您要么没有定义参数,要么在两者中都定义了它。您应该只在导出的 dll 项目中定义它。
    猜你喜欢
    • 2013-12-27
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    相关资源
    最近更新 更多