简体   繁体   English

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

[英]VisualStudio 2012 - two projects -> linker error (C++)

first my simple setup: I've got 2 VS2012 projects. 首先是我的简单设置:我有2个VS2012项目。 Now I want to use classes from Project B in Project A. I added Project B to A's project dependencie list and imported the headers where necessary. 现在,我想使用项目A中项目B的类。我将项目B添加到A的项目依赖项列表中,并在必要时导入了标头。 (eg #include"..\\src-pool\\Coords.h";). (例如#include“ .. \\ src-pool \\ Coords.h”;)。

So far, so good - no compiler errors. 到目前为止,一切都很好-没有编译器错误。 But when I try to build the project, I get some linker errors: 但是,当我尝试构建项目时,出现一些链接器错误:

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    

I'm sorry, this is the german version of VS. 抱歉,这是VS的德语版本。 "Verweis auf nicht aufgelöstes externes Symbol" means: Link to not resolved extern symbol. “ Verweis auf nichtaufgelöstesexternes Symbol”的意思是:链接到未解析的extern符号。

Any ideas? 有任何想法吗? =) =)


Done this (this is a class I want to export and use in the other project) 完成此操作(这是我要导出并在其他项目中使用的类)

Coords.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;

};

But I get a warning "inconsistent dll export" and the same errors. 但是我收到警告“不一致的dll导出”和相同的错误。 Sorry, I'm new to C++ 抱歉,我是C ++的新手


and I want to use it like this 我想这样使用

#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;

};

There are 3 things that need to happen for the linker to find the a class method: 链接器找到类方法需要发生三件事:

  1. You reference the right dll/project 您引用正确的dll /项目
  2. You have an implementation of that method in that dll/project. 您在该dll /项目中具有该方法的实现。
  3. It's exposed externally with a dll export ( __declspec(dllexport) ) 它通过dll导出( __declspec(dllexport) )在外部公开

Common practice for declaring an export in a header: 在标头中声明导出的常见做法:

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

class MyClass
{
     MYCLASSEXPORT MyClass();
}

Then you can just define that preprocessor argument in the exporting dll's preprocessor definitions. 然后,您可以在导出dll的预处理程序定义中定义该预处理程序参数。

In Visual studio: 在Visual Studio中:
Project properties -> Configuration properties -> C/C++ -> Preprocessor -> Prerpocessor deffinitios 项目属性->配置属性-> C / C ++->预处理器-> Prerpocessor deffinitios

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM