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.
source share