简体   繁体   English

extern“C”DLL:调试正常,Release抛出错误C2059

[英]extern “C” DLL: Debug is OK, Release throws Error C2059

I've got a DLL that I've created as a C++ Win32 application. 我有一个我作为C ++ Win32应用程序创建的DLL。 To prevent name mangling in my DLL, I have used the EXPORT definition defined below: 为了防止在我的DLL中进行名称修改,我使用了以下定义的EXPORT定义:

#ifndef EXPORT
#define EXPORT extern "C" __declspec(dllexport)
#endif

EXPORT int _stdcall SteadyFor(double Par[], double Inlet[], double Outlet[]);

To get this code to compile, I had to go into the project's Properties and set the C/C++ Calling Convention to __stdcall (/Gz) and set Compile As to Compile as C++ Code (/TP) . 为了编译这个代码,我必须进入项目的属性并将C / C ++ Calling Convention__stdcall(/ Gz)并将Compile As设置为编译为C ++代码(/ TP)

This worked in Debug mode, but Release mode is throwing error C2059: syntax error: 'string' on all of my EXPORT functions - even though I have configured the Release mode settings to be the same as the Debug settings. 这在调试模式下工作,但释放模式在我的所有EXPORT函数中抛出error C2059: syntax error: 'string' - 即使我已将Release模式设置配置为与Debug设置相同。

How do I get Release Mode to compile? 如何编译发布模式?

Regards, 问候,
~Joe 〜乔
(Developing under Visual Studio 2008 Professional) (在Visual Studio 2008 Professional下开发)

EDIT: 编辑:
A lot of comments about my #define, which does not appear to be causing any problems. 关于我的#define的很多评论,似乎没有引起任何问题。

To eliminate the confusion, my header file has been rewritten as follows: 为了消除这种混淆,我的头文件已被重写如下:

#ifndef coilmodel_h
#define coilmodel_h

extern "C" __declspec(dllexport) int _stdcall steadyFor(double Par[], double Inlet[], double Outlet[], char* FileIn, char* FileOut);

#endif

That is all of it. 这就是全部。

The error is: 错误是:
Description error C2059: syntax error: 'string' 描述 error C2059: syntax error: 'string'
File coilmodel.h 文件 coilmodel.h
Line 4 4

Again, this error only appears in Release mode, not Debug mode. 同样,此错误仅出现在“释放”模式下,而不是“调试”模式。
Project is a C++ Win32 DLL application. Project是一个C ++ Win32 DLL应用程序。

If your source file has a .c extension, the compiler you are using will compile it as C (not C++) and produce that error on the extern "C" . 如果您的源文件具有.c扩展名,则您使用的编译器将其编译为C(而不是C ++)并在extern "C"上生成该错误。 If that is the case, then you need to use the /TP switch as you already noted or rename the file to .cpp . 如果是这种情况,那么您需要使用已经记下的/TP开关,或者将文件重命名为.cpp The other solution is to put #ifdefs around the extern: 另一个解决方案是将#ifdefs放在extern周围:

#ifdef __cplusplus
extern "C"
#endif

I would guess EXPORT is defined as something else in Release builds. 我猜想EXPORT在Release版本中定义为其他东西。 Since you've got an #ifndef around your definition, that won't do anything if it is already defined, then you get something else (maybe a string?) pasted at the beginning of your function declarations. 因为你的定义周围有一个#ifndef ,如果已经定义了它就不会做任何事情,那么你会在函数声明的开头粘贴其他东西(可能是一个字符串?)。

So maybe try something like this: 所以也许尝试这样的事情:

#ifdef EXPORT
    #error EXPORT already defined!
#else
    #define EXPORT extern "C" __declspec(dllexport)
#endif

Forcing Compile As to Compile as C++ Code (/TP) - did you set this on all build configurations - debug/release x 32/x64 etc. I avoid using this option, much easier to name the file appropriately for the compiler to choose automatically. 强制Compile As为编译为C ++代码(/ TP) - 你是否在所有构建配置上设置了这个 - debug / release x 32 / x64等。我避免使用这个选项,更容易命名文件,编译器自动选择。

You only need the "C" part of extern "C" if the file is C++ to turn off the name mangling. 如果文件是C ++来关闭名称修改,你只需要extern "C""C"部分。

I prefer to arrange the shared public header using this format, so you can include in C/C++ internally or externally. 我更喜欢使用这种格式安排共享公共标头,因此您可以在内部或外部包含在C / C ++中。

#ifdef __cplusplus
# define NOMANGLE extern "C"
#else
# define NOMANGLE
#endif

#ifdef EXPORT_BUILD
# define EXPORT NOMANGLE __declspec(dllexport)
#else
# define EXPORT NOMANGLE __declspec(dllimport)
#endif

不可能,但要确保dllexport或_stdcall不是#defined ...

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

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