Error: wrong number of arguments specified for ‘constructor’ attribute
Before the actual implementation, i wrote a small prototype code, and put a class constructor and ctor constructor in the same file, to see if the ctor would execute first, which is my actual implementation.
However, i am facing an error. Here is the code:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
extern "C" void startMe(void) __attribute__ ((constructor(1)));
extern "C" void ending(void) __attribute__ ((destructor));
class Test {
public:
Test()
{
cout << "This is test constructor" << endl;
}
};
int main()
{
Test();
printf("Now main called\n");
}
void startMe(void)
{
printf("Start me called before main\n");
}
void ending(void)
{
printf("Destructor called\n");
}
--
Output:
$ g++ constructor1.cc
constructor1.cc:10: error: wrong number of arguments specified for ‘constructor’ attribute
However, when i remove the constructor priority, it compiles and runs fine. That is, i do:
extern "C" void startMe(void) __attribute__ ((constructor));
Why is it so? How to give priority?
Please help me. My idea is "ctor" should be executed f开发者_运维技巧irst, then the other (Test) constructor. The same reason, i have put ctor as a priority 1.
Compiling your program as is yields:
warning: constructor priorities from 0 to 100 are reserved for the implementation
Changing the priority from 1 to 101 gets rid of the warning and the executable produces:
Start me called before main
This is test constructor
Now main called
Destructor called
This is using GCC 4.5
wrong number of arguments specified for ‘constructor’ attribute
It looks like you are using a downlevel version of GCC.
According to the GCC 4.2.1 docs, the following are the relevant GCC 4.2.1 Function Attributes:
constructor
destructor
The constructor attribute causes the function to be called automatically before execution entersmain ()
...
And the relevant GCC 4.3.0 Function Attributes:
constructor
destructor
constructor (priority)
destructor (priority)
The constructor attribute causes the function to be called automatically before execution entersmain ()
...
The solution is to use GCC 4.3 or above.
I'm currently testing some software on OpenBSD 5.7, and it ships with the GCC 4.2.1 compiler. We also support CentOS 5, and that ships with the GCC 4.1 compiler. Here's what out code looks like:
// INIT_PRIORITY manages initialization of C++ static objects. Under GCC,
// the library uses init_priority attribute in the range [INIT_PRIORITY,
// INIT_PRIORITY+100]. Under Windows, INIT_PRIORITY enlists
// "#pragma init_seg(lib)". Undefine or set to 0 to disable it.
#define INIT_PRIORITY 250
#ifdef __GNUC__
# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif
#ifdef __clang__
# define CLANG_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#endif
...
#if __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
DLL void API DetectX86Features() __attribute__ ((constructor (INIT_PRIORITY + 50)));
DLL bool API CpuId(word32 input, word32 *output);
#elif __GNUC__ && INIT_PRIORITY
DLL void API DetectX86Features() __attribute__ ((constructor));
DLL bool API CpuId(word32 input, word32 *output);
#else
DLL void API DetectX86Features();
DLL bool API CpuId(word32 input, word32 *output);
#endif
You should probably create an additional class, like Initialization
, and put startMe
in the constructor and ending
in the destructor. Then, create a static instance of the C++ object, like Initialization init;
.
To avoid the static initialization order fiasco, you should use init_priority
(also see this Stack Overflow question and Clarification of attribute init_priority on the GCC mailing list). init_priority
has been around since at least GCC 3.2.
精彩评论