大网数据广告活动
Web服务器与PHP

C2491 error when defining data as dllimport

· 52CE 我爱测

Describes that you receive a Compiler C2491 error message when you try to define data members as dllimport functions.

This article helps you resolve the compiler C2491 error that occurs when you try to define data members as dllimport functions.

Original product version:   Visual C++
Original KB number:   815647

Symptom

You cannot apply the __declspec(dllimport) keyword to implement a function. For example, when you try to define data members (including static data members and functions) as dllimport functions, you receive the following compiler C2491 error message:

'identifier' : definition of dllimport function not allowed

Cause and resolution

You can only apply the __declspec(dllimport) keyword to declarations. You cannot apply the __declspec(dllimport) keyword to implement functions. The purpose of this keyword is to declare the implementation of a function by a DLL. Similarly, if you apply the __declspec(dllimport) keyword to a data member, you receive the initial data from a DLL. Therefore, you can't assign a value in your code initially.

You receive the compiler C2491 error when you try to compile the following code:

// function definition
void __declspec(dllimport) funcB() 
{
    // error C2491: 'funcB' : definition of dllimport function not allowed
}

This behavior occurs because you defined the function implementation as dllimport. To avoid this compiler error, don't define the function, but instead declare the function as follows:

// function declaration
void __declspec(dllimport) funcB(); // ok
int main()
{
}

Similarly, you receive the compiler C2491 error when you try to compile the following code:

//defining data member
extern __declspec(dllimport) int code = 1;
// error C2491: 'code' : definition of dllimport data not allowed

You receive this error message because you defined the data member as dllimport. To avoid this compiler error, don't define the data member, but instead declare the data member as follows:

// declaring data member
extern __declspec(dllimport) int code; // ok

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 04/20/2020
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。