What is __declspec and when do I need to use it?
I have seen instances of __declspec
in the code that I am reading. What is it? And when would I need to use this construct?
c++
add a comment |
I have seen instances of __declspec
in the code that I am reading. What is it? And when would I need to use this construct?
c++
add a comment |
I have seen instances of __declspec
in the code that I am reading. What is it? And when would I need to use this construct?
c++
I have seen instances of __declspec
in the code that I am reading. What is it? And when would I need to use this construct?
c++
c++
edited Nov 23 '18 at 16:24
kvantour
8,39831230
8,39831230
asked Feb 17 '10 at 21:42
Scott JScott J
1,06741313
1,06741313
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
This is a Microsoft specific extension to the C++ language which allows you to attribute a type or function with storage class information.
Documentation
__declspec (C++)
12
Thanks - but what would I use it for?
– Scott J
Feb 17 '10 at 21:45
4
For declaring COM interfaces and classes, for example, you use __declspec(uuid), for exporting functions sans a DEF file you use __declspec(dllexport), etc. The full list is quite long.
– Seva Alekseyev
Feb 17 '10 at 21:49
1
@ScottJ I constantly use it for properties: __declspec(property(get=X put=X)) data-type identifier. MSDN has more details of course. As a "C# to C/C++ programmer" it's a bliss!
– MasterMastic
Aug 14 '12 at 21:29
add a comment |
The canonical examples are __declspec(dllimport)
and __declspec(dllexport)
, which instruct the linker to import and export (respectively) a symbol from or to a DLL.
// header
__declspec(dllimport) void foo();
// code - this calls foo() somewhere in a DLL
foo();
(__declspec(..)
just wraps up Microsoft's specific stuff - to achieve compatibility, one would usually wrap it away with macros)
9
how do you know wich dll dllimport is pointing at?
– tetris
May 14 '12 at 10:21
6
@tetris, you don't know from the code. The decision is made by the linker, who will pick the first.lib
it finds that has a matching exported symbol.
– Euro Micelli
Nov 1 '13 at 10:57
add a comment |
It is mostly used for importing symbols from / exporting symbols to a shared library (DLL). Both Visual C++ and GCC compilers support __declspec(dllimport)
and __declspec(dllexport)
. Other uses (some Microsoft-only) are documented in the MSDN.
add a comment |
Another example to illustrate the __declspec keyword:
When you are writing a Windows Kernel Driver, sometimes you want to write your own prolog/epilog code sequences using inline assembler code, so you could declare your function with the naked attribute.
__declspec( naked ) int func( formal_parameters ) {}
Or
#define Naked __declspec( naked )
Naked int func( formal_parameters ) {}
Please refer to naked (C++)
add a comment |
Essentially, it's the way Microsoft introduces its C++ extensions so that they won't conflict with future extensions of standard C++. With __declspec, you can attribute a function or class; the exact meaning varies depending on the nature of __declspec. __declspec(naked), for example, suppresses prolog/epilog generation (for interrupt handlers, embeddable code, etc), __declspec(thread) makes a variable thread-local, and so on.
The full list of __declspec attributes is available on MSDN, and varies by compiler version and platform.
1
Considering non-microsoft compilers likeGCC 4.2
, that offer alternative in addition to their__attribute__ ((dllexport))
to__declspec(dllexport)
, is it fair to call__declspec
, a Microsoft-only extension?
– user2338150
Jul 4 '17 at 6:03
add a comment |
I know it's been eight years but I wanted to share this piece of code found in MRuby that shows how __declspec()
can bee used at the same level as the export keyword
.
/** Declare a public MRuby API function. */
#if defined(MRB_BUILD_AS_DLL)
#if defined(MRB_CORE) || defined(MRB_LIB)
# define MRB_API __declspec(dllexport)
#else
# define MRB_API __declspec(dllimport)
#endif
#else
# define MRB_API extern
#endif
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2284610%2fwhat-is-declspec-and-when-do-i-need-to-use-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a Microsoft specific extension to the C++ language which allows you to attribute a type or function with storage class information.
Documentation
__declspec (C++)
12
Thanks - but what would I use it for?
– Scott J
Feb 17 '10 at 21:45
4
For declaring COM interfaces and classes, for example, you use __declspec(uuid), for exporting functions sans a DEF file you use __declspec(dllexport), etc. The full list is quite long.
– Seva Alekseyev
Feb 17 '10 at 21:49
1
@ScottJ I constantly use it for properties: __declspec(property(get=X put=X)) data-type identifier. MSDN has more details of course. As a "C# to C/C++ programmer" it's a bliss!
– MasterMastic
Aug 14 '12 at 21:29
add a comment |
This is a Microsoft specific extension to the C++ language which allows you to attribute a type or function with storage class information.
Documentation
__declspec (C++)
12
Thanks - but what would I use it for?
– Scott J
Feb 17 '10 at 21:45
4
For declaring COM interfaces and classes, for example, you use __declspec(uuid), for exporting functions sans a DEF file you use __declspec(dllexport), etc. The full list is quite long.
– Seva Alekseyev
Feb 17 '10 at 21:49
1
@ScottJ I constantly use it for properties: __declspec(property(get=X put=X)) data-type identifier. MSDN has more details of course. As a "C# to C/C++ programmer" it's a bliss!
– MasterMastic
Aug 14 '12 at 21:29
add a comment |
This is a Microsoft specific extension to the C++ language which allows you to attribute a type or function with storage class information.
Documentation
__declspec (C++)
This is a Microsoft specific extension to the C++ language which allows you to attribute a type or function with storage class information.
Documentation
__declspec (C++)
edited Dec 13 '17 at 4:02
Mark Benningfield
1,32141421
1,32141421
answered Feb 17 '10 at 21:44
JaredParJaredPar
570k11910581342
570k11910581342
12
Thanks - but what would I use it for?
– Scott J
Feb 17 '10 at 21:45
4
For declaring COM interfaces and classes, for example, you use __declspec(uuid), for exporting functions sans a DEF file you use __declspec(dllexport), etc. The full list is quite long.
– Seva Alekseyev
Feb 17 '10 at 21:49
1
@ScottJ I constantly use it for properties: __declspec(property(get=X put=X)) data-type identifier. MSDN has more details of course. As a "C# to C/C++ programmer" it's a bliss!
– MasterMastic
Aug 14 '12 at 21:29
add a comment |
12
Thanks - but what would I use it for?
– Scott J
Feb 17 '10 at 21:45
4
For declaring COM interfaces and classes, for example, you use __declspec(uuid), for exporting functions sans a DEF file you use __declspec(dllexport), etc. The full list is quite long.
– Seva Alekseyev
Feb 17 '10 at 21:49
1
@ScottJ I constantly use it for properties: __declspec(property(get=X put=X)) data-type identifier. MSDN has more details of course. As a "C# to C/C++ programmer" it's a bliss!
– MasterMastic
Aug 14 '12 at 21:29
12
12
Thanks - but what would I use it for?
– Scott J
Feb 17 '10 at 21:45
Thanks - but what would I use it for?
– Scott J
Feb 17 '10 at 21:45
4
4
For declaring COM interfaces and classes, for example, you use __declspec(uuid), for exporting functions sans a DEF file you use __declspec(dllexport), etc. The full list is quite long.
– Seva Alekseyev
Feb 17 '10 at 21:49
For declaring COM interfaces and classes, for example, you use __declspec(uuid), for exporting functions sans a DEF file you use __declspec(dllexport), etc. The full list is quite long.
– Seva Alekseyev
Feb 17 '10 at 21:49
1
1
@ScottJ I constantly use it for properties: __declspec(property(get=X put=X)) data-type identifier. MSDN has more details of course. As a "C# to C/C++ programmer" it's a bliss!
– MasterMastic
Aug 14 '12 at 21:29
@ScottJ I constantly use it for properties: __declspec(property(get=X put=X)) data-type identifier. MSDN has more details of course. As a "C# to C/C++ programmer" it's a bliss!
– MasterMastic
Aug 14 '12 at 21:29
add a comment |
The canonical examples are __declspec(dllimport)
and __declspec(dllexport)
, which instruct the linker to import and export (respectively) a symbol from or to a DLL.
// header
__declspec(dllimport) void foo();
// code - this calls foo() somewhere in a DLL
foo();
(__declspec(..)
just wraps up Microsoft's specific stuff - to achieve compatibility, one would usually wrap it away with macros)
9
how do you know wich dll dllimport is pointing at?
– tetris
May 14 '12 at 10:21
6
@tetris, you don't know from the code. The decision is made by the linker, who will pick the first.lib
it finds that has a matching exported symbol.
– Euro Micelli
Nov 1 '13 at 10:57
add a comment |
The canonical examples are __declspec(dllimport)
and __declspec(dllexport)
, which instruct the linker to import and export (respectively) a symbol from or to a DLL.
// header
__declspec(dllimport) void foo();
// code - this calls foo() somewhere in a DLL
foo();
(__declspec(..)
just wraps up Microsoft's specific stuff - to achieve compatibility, one would usually wrap it away with macros)
9
how do you know wich dll dllimport is pointing at?
– tetris
May 14 '12 at 10:21
6
@tetris, you don't know from the code. The decision is made by the linker, who will pick the first.lib
it finds that has a matching exported symbol.
– Euro Micelli
Nov 1 '13 at 10:57
add a comment |
The canonical examples are __declspec(dllimport)
and __declspec(dllexport)
, which instruct the linker to import and export (respectively) a symbol from or to a DLL.
// header
__declspec(dllimport) void foo();
// code - this calls foo() somewhere in a DLL
foo();
(__declspec(..)
just wraps up Microsoft's specific stuff - to achieve compatibility, one would usually wrap it away with macros)
The canonical examples are __declspec(dllimport)
and __declspec(dllexport)
, which instruct the linker to import and export (respectively) a symbol from or to a DLL.
// header
__declspec(dllimport) void foo();
// code - this calls foo() somewhere in a DLL
foo();
(__declspec(..)
just wraps up Microsoft's specific stuff - to achieve compatibility, one would usually wrap it away with macros)
edited Jan 11 '12 at 15:53
Robino
2,19011832
2,19011832
answered Feb 17 '10 at 21:46
Alexander GesslerAlexander Gessler
38.7k570113
38.7k570113
9
how do you know wich dll dllimport is pointing at?
– tetris
May 14 '12 at 10:21
6
@tetris, you don't know from the code. The decision is made by the linker, who will pick the first.lib
it finds that has a matching exported symbol.
– Euro Micelli
Nov 1 '13 at 10:57
add a comment |
9
how do you know wich dll dllimport is pointing at?
– tetris
May 14 '12 at 10:21
6
@tetris, you don't know from the code. The decision is made by the linker, who will pick the first.lib
it finds that has a matching exported symbol.
– Euro Micelli
Nov 1 '13 at 10:57
9
9
how do you know wich dll dllimport is pointing at?
– tetris
May 14 '12 at 10:21
how do you know wich dll dllimport is pointing at?
– tetris
May 14 '12 at 10:21
6
6
@tetris, you don't know from the code. The decision is made by the linker, who will pick the first
.lib
it finds that has a matching exported symbol.– Euro Micelli
Nov 1 '13 at 10:57
@tetris, you don't know from the code. The decision is made by the linker, who will pick the first
.lib
it finds that has a matching exported symbol.– Euro Micelli
Nov 1 '13 at 10:57
add a comment |
It is mostly used for importing symbols from / exporting symbols to a shared library (DLL). Both Visual C++ and GCC compilers support __declspec(dllimport)
and __declspec(dllexport)
. Other uses (some Microsoft-only) are documented in the MSDN.
add a comment |
It is mostly used for importing symbols from / exporting symbols to a shared library (DLL). Both Visual C++ and GCC compilers support __declspec(dllimport)
and __declspec(dllexport)
. Other uses (some Microsoft-only) are documented in the MSDN.
add a comment |
It is mostly used for importing symbols from / exporting symbols to a shared library (DLL). Both Visual C++ and GCC compilers support __declspec(dllimport)
and __declspec(dllexport)
. Other uses (some Microsoft-only) are documented in the MSDN.
It is mostly used for importing symbols from / exporting symbols to a shared library (DLL). Both Visual C++ and GCC compilers support __declspec(dllimport)
and __declspec(dllexport)
. Other uses (some Microsoft-only) are documented in the MSDN.
edited Sep 24 '16 at 9:43
K DawG
6,77182357
6,77182357
answered Feb 17 '10 at 21:47
AndiDogAndiDog
48.9k12132184
48.9k12132184
add a comment |
add a comment |
Another example to illustrate the __declspec keyword:
When you are writing a Windows Kernel Driver, sometimes you want to write your own prolog/epilog code sequences using inline assembler code, so you could declare your function with the naked attribute.
__declspec( naked ) int func( formal_parameters ) {}
Or
#define Naked __declspec( naked )
Naked int func( formal_parameters ) {}
Please refer to naked (C++)
add a comment |
Another example to illustrate the __declspec keyword:
When you are writing a Windows Kernel Driver, sometimes you want to write your own prolog/epilog code sequences using inline assembler code, so you could declare your function with the naked attribute.
__declspec( naked ) int func( formal_parameters ) {}
Or
#define Naked __declspec( naked )
Naked int func( formal_parameters ) {}
Please refer to naked (C++)
add a comment |
Another example to illustrate the __declspec keyword:
When you are writing a Windows Kernel Driver, sometimes you want to write your own prolog/epilog code sequences using inline assembler code, so you could declare your function with the naked attribute.
__declspec( naked ) int func( formal_parameters ) {}
Or
#define Naked __declspec( naked )
Naked int func( formal_parameters ) {}
Please refer to naked (C++)
Another example to illustrate the __declspec keyword:
When you are writing a Windows Kernel Driver, sometimes you want to write your own prolog/epilog code sequences using inline assembler code, so you could declare your function with the naked attribute.
__declspec( naked ) int func( formal_parameters ) {}
Or
#define Naked __declspec( naked )
Naked int func( formal_parameters ) {}
Please refer to naked (C++)
edited Mar 30 '16 at 14:13
DanielV
76211741
76211741
answered May 20 '11 at 6:22
HackNoneHackNone
421612
421612
add a comment |
add a comment |
Essentially, it's the way Microsoft introduces its C++ extensions so that they won't conflict with future extensions of standard C++. With __declspec, you can attribute a function or class; the exact meaning varies depending on the nature of __declspec. __declspec(naked), for example, suppresses prolog/epilog generation (for interrupt handlers, embeddable code, etc), __declspec(thread) makes a variable thread-local, and so on.
The full list of __declspec attributes is available on MSDN, and varies by compiler version and platform.
1
Considering non-microsoft compilers likeGCC 4.2
, that offer alternative in addition to their__attribute__ ((dllexport))
to__declspec(dllexport)
, is it fair to call__declspec
, a Microsoft-only extension?
– user2338150
Jul 4 '17 at 6:03
add a comment |
Essentially, it's the way Microsoft introduces its C++ extensions so that they won't conflict with future extensions of standard C++. With __declspec, you can attribute a function or class; the exact meaning varies depending on the nature of __declspec. __declspec(naked), for example, suppresses prolog/epilog generation (for interrupt handlers, embeddable code, etc), __declspec(thread) makes a variable thread-local, and so on.
The full list of __declspec attributes is available on MSDN, and varies by compiler version and platform.
1
Considering non-microsoft compilers likeGCC 4.2
, that offer alternative in addition to their__attribute__ ((dllexport))
to__declspec(dllexport)
, is it fair to call__declspec
, a Microsoft-only extension?
– user2338150
Jul 4 '17 at 6:03
add a comment |
Essentially, it's the way Microsoft introduces its C++ extensions so that they won't conflict with future extensions of standard C++. With __declspec, you can attribute a function or class; the exact meaning varies depending on the nature of __declspec. __declspec(naked), for example, suppresses prolog/epilog generation (for interrupt handlers, embeddable code, etc), __declspec(thread) makes a variable thread-local, and so on.
The full list of __declspec attributes is available on MSDN, and varies by compiler version and platform.
Essentially, it's the way Microsoft introduces its C++ extensions so that they won't conflict with future extensions of standard C++. With __declspec, you can attribute a function or class; the exact meaning varies depending on the nature of __declspec. __declspec(naked), for example, suppresses prolog/epilog generation (for interrupt handlers, embeddable code, etc), __declspec(thread) makes a variable thread-local, and so on.
The full list of __declspec attributes is available on MSDN, and varies by compiler version and platform.
answered Feb 17 '10 at 21:46
Seva AlekseyevSeva Alekseyev
44.6k16125218
44.6k16125218
1
Considering non-microsoft compilers likeGCC 4.2
, that offer alternative in addition to their__attribute__ ((dllexport))
to__declspec(dllexport)
, is it fair to call__declspec
, a Microsoft-only extension?
– user2338150
Jul 4 '17 at 6:03
add a comment |
1
Considering non-microsoft compilers likeGCC 4.2
, that offer alternative in addition to their__attribute__ ((dllexport))
to__declspec(dllexport)
, is it fair to call__declspec
, a Microsoft-only extension?
– user2338150
Jul 4 '17 at 6:03
1
1
Considering non-microsoft compilers like
GCC 4.2
, that offer alternative in addition to their __attribute__ ((dllexport))
to __declspec(dllexport)
, is it fair to call __declspec
, a Microsoft-only extension?– user2338150
Jul 4 '17 at 6:03
Considering non-microsoft compilers like
GCC 4.2
, that offer alternative in addition to their __attribute__ ((dllexport))
to __declspec(dllexport)
, is it fair to call __declspec
, a Microsoft-only extension?– user2338150
Jul 4 '17 at 6:03
add a comment |
I know it's been eight years but I wanted to share this piece of code found in MRuby that shows how __declspec()
can bee used at the same level as the export keyword
.
/** Declare a public MRuby API function. */
#if defined(MRB_BUILD_AS_DLL)
#if defined(MRB_CORE) || defined(MRB_LIB)
# define MRB_API __declspec(dllexport)
#else
# define MRB_API __declspec(dllimport)
#endif
#else
# define MRB_API extern
#endif
add a comment |
I know it's been eight years but I wanted to share this piece of code found in MRuby that shows how __declspec()
can bee used at the same level as the export keyword
.
/** Declare a public MRuby API function. */
#if defined(MRB_BUILD_AS_DLL)
#if defined(MRB_CORE) || defined(MRB_LIB)
# define MRB_API __declspec(dllexport)
#else
# define MRB_API __declspec(dllimport)
#endif
#else
# define MRB_API extern
#endif
add a comment |
I know it's been eight years but I wanted to share this piece of code found in MRuby that shows how __declspec()
can bee used at the same level as the export keyword
.
/** Declare a public MRuby API function. */
#if defined(MRB_BUILD_AS_DLL)
#if defined(MRB_CORE) || defined(MRB_LIB)
# define MRB_API __declspec(dllexport)
#else
# define MRB_API __declspec(dllimport)
#endif
#else
# define MRB_API extern
#endif
I know it's been eight years but I wanted to share this piece of code found in MRuby that shows how __declspec()
can bee used at the same level as the export keyword
.
/** Declare a public MRuby API function. */
#if defined(MRB_BUILD_AS_DLL)
#if defined(MRB_CORE) || defined(MRB_LIB)
# define MRB_API __declspec(dllexport)
#else
# define MRB_API __declspec(dllimport)
#endif
#else
# define MRB_API extern
#endif
answered Jun 14 '18 at 13:50
user9726814
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2284610%2fwhat-is-declspec-and-when-do-i-need-to-use-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown