草庐IT

c++ - Visual Studio 2013 dll 导出链接错误 (LNK2019/LNK1120)

coder 2024-02-24 原文

我知道之前有人问过类似的问题,但看起来在类导出方面与简单函数有所不同......我已经检查了所有这些解决方案,检查了所有建议,但看起来我仍然是遗漏了一些东西......

发生了什么:

  • 我有一个用 Visual Studio 2013 编写的主 C++ 项目,我想添加一个包含各种实用程序的 dll 库。我创建了一个虚拟的,基本上没有任何功能,但它无法编译:
    2>  TestSvc_i.c
    2>TestSvc.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall CUtils::CUtils(void)" (__imp_??0CUtils@@QAE@XZ) referenced in function _wWinMain@16
    2>C:\Work\TestSvc_root\Debug\TestSvc.exe : fatal error LNK1120: 1 unresolved externals
    ========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========

And the code in the main project looks like this:

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
    CUtils *a = new CUtils();
    delete a;

    return 1;
}

我有以下设置:

  • 用 C++ 和 Visual Studio 2013 编写的主项目;
  • 我使用的是 Unicode 字符集,运行时用于共享 dll;
  • _UNICODE 已定义;
  • 子系统:Windows (/SUBSYSTEM:WINDOWS);
  • 我想创建一个应该在共享 dll 中使用的实用程序库。代码如下所示:

实用程序.h:

#ifdef UTILS_EXPORTS
#define UTILS_API __declspec(dllexport)
#else
#define UTILS_API __declspec(dllimport)
#endif

// This class is exported from the Utils.dll
class UTILS_API CUtils {
public:
    CUtils(void);
};

实用程序.cpp:

#include "stdafx.h"
#include "Utils.h"

// This is the constructor of a class that has been exported.
// see Utils.h for the class definition
CUtils::CUtils()
{
    return;
}
  • 我检查了 dll 项目设置,基本上,它们都与主项目中的一样;
  • UTILS_EXPORTS 通常在 dll-project 中定义,所有定义都应该有 __declspec(dllexport)(如预期的那样,未在 UTILS_EXPORTS 中定义);
  • 我有以下预处理器定义:WIN32;_DEBUG;_WINDOWS;_USRDLL;UTILS_EXPORTS ;
  • dll-project 被添加为 master-project 的依赖;
  • dll 复制到 $(SolutionDir)\Debug 下,但不复制到 $(SolutionDir)\$(MasterProject)\Debug 下 - 我不知道为什么。我手动复制了,还是一样的问题;
  • 我在 Utils.dll 上运行了 DUMPBIN 实用程序,它看起来是这样的:
    C:\Work\TestSvc_root\Debug>DUMPBIN /EXPORTS /SYMBOLS Utils.dll
    Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.


    Dump of file Utils.dll

    File Type: DLL

      Section contains the following exports for Utils.dll

    00000000 characteristics
    53C632A8 time date stamp Wed Jul 16 10:07:04 2014
        0.00 version
           1 ordinal base
           2 number of functions
           2 number of names

    ordinal hint RVA      name

          1    0 00011154 ??0CUtils@@QAE@XZ = @ILT+335(??0CUtils@@QAE@XZ)
          2    1 000110C8 ??4CUtils@@QAEAAV0@ABV0@@Z = @ILT+195(??4CUtils@@QAEAAV0@ABV0@@Z)

    Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss
  • 我尝试“嗅探”UTILS_API 的值:
    #define DO_QUOTE(X)        #X
    #define QUOTE(X)           DO_QUOTE(X)
    #define MY_QUOTED_VAR      QUOTE(MYVARIABLE)

    #pragma message(QUOTE(UTILS_API))

它转储正确的值:当从 dll 中“使用”时,它转储 __declspec(dllexport),当从主项目中使用时,它转储 __declspec(dllimport)

那么,有什么提示吗?看起来有些东西与类导出定义/方法修饰有关......在以前的普通旧 Visual C++ 中,曾经有一个“def”文件,你可以在其中映射导出的名称......但在这个新版本中,我迷路了。

谢谢!

最佳答案

作为解决方法,您可以将 .lib 文件(在构建 .dll 期间生成)显式添加到主项目的链接器选项中的输入库列表中。

关于c++ - Visual Studio 2013 dll 导出链接错误 (LNK2019/LNK1120),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776853/

有关c++ - Visual Studio 2013 dll 导出链接错误 (LNK2019/LNK1120)的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  3. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  4. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  5. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  6. += 的 Ruby 方法 - 2

    有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=

  7. ruby - Sinatra + Heroku + Datamapper 使用 dm-sqlite-adapter 部署问题 - 2

    出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t

  8. ruby - Ruby 中字符串运算符 + 和 << 的区别 - 2

    我是Ruby和这个网站的新手。下面两个函数是不同的,一个在函数外修改变量,一个不修改。defm1(x)x我想确保我理解正确-当调用m1时,对str的引用被复制并传递给将其视为x的函数。运算符当调用m2时,对str的引用被复制并传递给将其视为x的函数。运算符+创建一个新字符串,赋值x=x+"4"只是将x重定向到新字符串,而原始str变量保持不变。对吧?谢谢 最佳答案 String#+::str+other_str→new_strConcatenation—ReturnsanewStringcontainingother_strconc

  9. ruby - rails 3.2.2(或 3.2.1)+ Postgresql 9.1.3 + Ubuntu 11.10 连接错误 - 2

    我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat

  10. ruby - 在 Ruby + Chef 中检查现有目录失败 - 2

    这是我在ChefRecipe中的一blockRuby:#ifdatadirdoesn'texist,moveoverthedefaultoneif!File.exist?("/vol/postgres/data")execute"mv/var/lib/postgresql/9.1/main/vol/postgres/data"end结果是:Executingmv/var/lib/postgresql/9.1/main/vol/postgres/datamv:inter-devicemovefailed:`/var/lib/postgresql/9.1/main'to`/vol/post

随机推荐