从另一个dll链接动态dll会导致错误

时间:2014-12-03 18:33:40

标签: c++ visual-c++ dll visual-studio-2013 dynamic-linking

我正在使用Visual C ++ 2013构建一个项目,其中我有一个所有其他项目应包含的全局共享库。该库项目只包含两个名为“utils.h”和“utils.cpp”的文件。

在utils.h文件中,我声明了以下代码。

#ifndef CLASS_UTIL
#define CLASS_UTIL

#ifdef _WINDLL
#if defined(LIBCIIEXPORTSHARED_STATIC)
#define LIBCIIEXPORTSHARED_API
#elif defined(LIBCIIEXPORTSHARED_EXPORTS)
#define LIBCIIEXPORTSHARED_API __declspec(dllexport)
#else
#define LIBCIIEXPORTSHARED_API __declspec(dllimport)
#endif /* LIBCIIFILE_STATIC */
#else //NOT WINDOWS DLL
#define LIBCIIEXPORTSHARED_API 
#endif

#include "options.h"
#include "global_constants.h"
#include <stdlib.h>
#include <string>
#include <sstream>
#include <math.h>
#include <iomanip>
using namespace std;

LIBCIIEXPORTSHARED_API double _cdecl string_to_double(const std::string& s);
LIBCIIEXPORTSHARED_API double _cdecl strict_str2double(char* str);
LIBCIIEXPORTSHARED_API double _cdecl getParNm(int num, string* element);
LIBCIIEXPORTSHARED_API double _cdecl calcDistance(double* delta);
LIBCIIEXPORTSHARED_API double _cdecl calcDistance(double* coords1, double* coords2);
LIBCIIEXPORTSHARED_API double _cdecl calc_angle(double*, double*);
LIBCIIEXPORTSHARED_API void _cdecl correct_coordinates(double* coords, Options_t opt);
LIBCIIEXPORTSHARED_API double _cdecl getDN(double diameter, Options_t opt);

LIBCIIEXPORTSHARED_API string _cdecl get_support_direction(double *delta, string support_type, Options_t opt);
LIBCIIEXPORTSHARED_API string _cdecl getSupportDesc(double id);
LIBCIIEXPORTSHARED_API void _cdecl eraseSupport(string element[]);
LIBCIIEXPORTSHARED_API void _cdecl set_control_limits(string element[]);
LIBCIIEXPORTSHARED_API void _cdecl load_default_diameter_par(double *&nominal, double *&real, int &array_size);

#endif

然后在项目设置中,我在预处理器定义中包含 LIBCIIEXPORTSHARED_EXPORTS

在我的第二个项目,这是另一个动态dll,我只包括utils.h 没有定义 LIBCIIEXPORTSHARED_EXPORTS ,我得到以下错误。

1>database_table.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) double __cdecl getParNm(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (__imp_?getParNm@@YANHPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: void __thiscall DBtable::set_db_data(class Element_Manager *,class Bend *,struct Options_t)" (?set_db_data@DBtable@@QAEXPAVElement_Manager@@PAVBend@@UOptions_t@@@Z)
1>element.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) double __cdecl getParNm(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (__imp_?getParNm@@YANHPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>database_table.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) double __cdecl calcDistance(double *)" (__imp_?calcDistance@@YANPAN@Z) referenced in function "public: void __thiscall DBtable::set_db_data(class Element_Manager *,class Bend *,struct Options_t)" (?set_db_data@DBtable@@QAEXPAVElement_Manager@@PAVBend@@UOptions_t@@@Z)
1>database_table.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) double __cdecl getDN(double,struct Options_t)" (__imp_?getDN@@YANNUOptions_t@@@Z) referenced in function "public: void __thiscall DBtable::set_db_data(class Element_Manager *,class Bend *,struct Options_t)" (?set_db_data@DBtable@@QAEXPAVElement_Manager@@PAVBend@@UOptions_t@@@Z)
1>element.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl correct_coordinates(double *,struct Options_t)" (__imp_?correct_coordinates@@YAXPANUOptions_t@@@Z) referenced in function "public: void __thiscall Element_Manager::set_data_from_file(class DBtable *,class CIIFile *,struct Options_t)" (?set_data_from_file@Element_Manager@@QAEXPAVDBtable@@PAVCIIFile@@UOptions_t@@@Z)
1>C:\Users\Nando\Documents\Projetos\05-rmc-ciiexport\code\Debug\libciifile++.dll : fatal error LNK1120: 4 unresolved externals

我已检查过两个项目都设置为使用Unicode字符集,第一个项目被标记为第二个项目的项目依赖项。

我还检查了.h目录是否包含在项目设置中的其他包含目录

我需要在visual studio设置上更改哪些内容才能链接DLL?

提前谢谢。

2 个答案:

答案 0 :(得分:1)

构建DLL时,它会生成.lib文件。您需要将此lib文件链接到将使用DLL的每个项目。 lib包含满足链接器的存根函数。

答案 1 :(得分:1)

您缺少的是dll的 lib 文件所在的目录,以及要链接的lib文件的名称

添加lib文件的路径。通常(但不是必需的)与您尝试链接的dll位于同一目录中。

配置属性 - &gt;链接器 - &gt;一般 - &gt;其他图书馆馆藏

将lib 文件添加到依赖项列表

配置属性 - &gt;链接器 - &gt;输入 - &gt;附加依赖