Error: Wrong number of arguments specified for constructor attribute

Before the actual implementation, I wrote a little prototype code and put the class constructor and ctor constructor in the same file to see if ctor will be executed first, which is my actual implementation.

However, I ran into 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 priority of the constructor, it compiles and works fine. That is, I do:

 extern "C" void startMe(void) __attribute__ ((constructor)); 

Why is that? How to give priority?

Please help me. My idea of ​​"ctor" should be executed first, then another (Test) constructor. For the same reason, I set ctor as priority 1.

+4
source share
2 answers

Compiling your program as results:

 warning: constructor priorities from 0 to 100 are reserved for the implementation 

Changing the priority from 1 to 101 eliminates the warning, and the executable produces:

  Start me called before main This is test constructor Now main called Destructor called 

It uses GCC 4.5

+1
source

invalid number of arguments specified for constructor attribute

It looks like you are using a younger version of GCC.

According to GCC 4.2.1 documents, the corresponding GCC 4.2.1 function attributes are listed below:

Constructor
destructor
The constructor attribute causes the function to call automatically before execution enters main () ...

And the corresponding GCC 4.3.0 Function Attributes :

Constructor
destructor
constructor (priority)
destructor (priority)
The constructor attribute causes the function to call automatically before execution enters main () ...

The solution is to use GCC 4.3 or higher.

I am currently testing some software on OpenBSD 5.7 and it comes with the GCC 4.2.1 compiler. We also support CentOS 5 and ship the GCC 4.1 compiler. Here is the code that looks like this:

 // 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, such as Initialization , and put startMe in the constructor and ending in the destructor. Then create a static instance of the C ++ object, such as Initialization init; .

To avoid the static initialization fiasco order , you should use init_priority (also see this question on stack overflow and Clarification of the init_priority attribute on the GCC mailing list). init_priority existed since at least GCC 3.2 .

0
source

All Articles