English 中文(简体)
在C建立一个DL,并将其与C++项目联系起来
原标题:Create a DLL in C and link it from a C++ project

As per title, I m trying to build a DLL using C and link it from a C++ project. I read and followed different tutorials on internet but everytime there is something missing and I don t understand what.

在此,我采取了以下步骤:

我创建了一个新的Win32项目,名为testlib,然后从主人那里选择了“DL”和“Empty Project”。

添加一个标题:

//testlib.h

#include <stdio.h>

__declspec(dllexport) void hello();

添加一个来源;由于我希望它成为一C来文方,我读一读,应该简单地将C.c.的档案改成一。

//testlib.c

#include "testlib.h"

void hello() {
    printf("DLL hello() called
");
}

建设。

现在,我想在另一个项目中利用我的有益助手。

然后: new project (testlibUse). This time I selected "Empty project".
No need to add an header, just created a cpp source

//main.cpp
#include <testlib.h>

int main() {
    hello();
}

然后:

  • 我在册子上添加了一条路:testlib.dll in Property->VC++Directories-> 可执行目录

  • 我在册子上添加了一条路:testlib.h in Property->VC++Directories->IncludeDirectories

  • 我添加了以下途径:房地产和建筑中的“条码”:;Linker->Input->Additional Dependencies

我试图建设,但我犯了一个联系错误:

LINK : C:path estlibUseDebug estlibUse.exe not found or not built by the last incremental link; performing full link
main.obj : error LNK2019: unresolved external symbol "void __cdecl hello(void)" (?hello@@YAXXZ) referenced in function _main
C:path estlibUseDebug estlibUse.exe : fatal error LNK1120: 1 unresolved externals

如果我回到<条码>测试lib,更名为<条码>测试lib.c,载于<条码>测试校准/代码>,并重建票价,那么,我可以建造<条码>测试校准/代码>,但我在操作时有“没有发现”错误。

I tried also to change the configurations of both projects in "Release" (changing the path where needed), but nothing changed.

Sorry for the long post but I think it was necessary to write down exactly what I did.

任何建议?

此外,如果我想要在Qt项目中使用我的飞机,我是否需要改变配置参数吗?

最佳回答

You have several problems:

  1. The header file should mark the functions as exported when being compiled in the DLL but imported when being compiled by a library user.
  2. The header file should wrap the function declarations in an extern "C" block when being compiled as C++ to ensure that the names do not get mangled
  3. The DLL is not on your executable s library search path, so it can t be found at runtime.

为了确定第(1)和(2)条,把你的头盔改成:

#ifdef __cplusplus
extern "C" {
#endif

// Assume this symbol is only defined by your DLL project, so we can either
// export or import the symbols as appropriate
#if COMPILING_MY_TEST_DLL
#define TESTLIB_EXPORT __declspec(dllexport)
#else
#define TESTLIB_EXPORT __declspec(dllimport)
#endif

TESTLIB_EXPORT void hello();
// ... more function declarations, marked with TESTLIB_EXPORT

#ifdef __cplusplus
}
#endif

更正(3),将《刑法》与你的可起诉档案翻一番。 http://msdn.microsoft.com/en-us/library/7d83bc 1828v=vs.80%29.aspx>MS<0/a>,详细描述如何搜寻DLs。 你的最佳解决办法是把你的DLL复制到你可起诉的档案馆。 你们要么能够以人工的方式这样做,要么在你的项目中增加一个在后实施的步骤,对你们来说就是这样。

问题回答

页: 1 包括:

extern "C" {
    #include <testlib.h>
}

此处采用C++可包含的贵方档案方法。 确保在< 预处理环境内建立<>TESTLIB_EXPORTS。 在包括该船长使用DLL的项目中,船长将宣布这些功能为进口而不是出口。

<代码>cplus 警卫将告诉编辑人员使用C国名勋章而不是C++国名校正来进口你的职能。

#include <stdio.h>

#ifdef TESTLIB_EXPORTS
#define TESTLIB_API __declspec(dllexport)
#else
#define TESTLIB_API __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

TESTLIB_API void hello();
/* other prototypes here */

#ifdef __cplusplus
}
#endif




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签