Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

__attribute__(constructor) alternative

Jose_M_Monsalve_Diaz
1,807 Views

Hi, 

I wrote a program that runs on Clang and GCC which relies on a function being called before the main. For this I use the __attribute__(compiler) support that is available in the other two compilers. 

Is there an intel compiler alternative to this?

Thanks

0 Kudos
6 Replies
RahulV_intel
Moderator
1,807 Views

Hi,

ICC allows __attribute__() support for user defined functions.

For instance, refer to the below code that allows you to call a function(with __attribute__ support) before main().

#include<iostream>

//Function before main
void beforeMain(void) __attribute__((constructor));

// implementation of beforeMain
void beforeMain(void) {
        std::cout << "Hello before main()\n";
}

int main (void) {
        std::cout << "hello\n";
    return 0;
}
/* Steps to compile/run
icpc filename.cpp
./a.out */

Let us know if you were successful in implementing "__attribute__" in your code whilst using icc/icpc.

 

--Rahul

0 Kudos
Jose_M_Monsalve_Diaz
1,807 Views

Rahul, Thanks for your answer. I see, I actually tried it on a single source and it worked. 

I could not find it documented and my code uses it and it does not seem to execute at the beginning of the program. I am trying to find a simpler reproducible of the code I have. I will report back.

 

0 Kudos
Jose_M_Monsalve_Diaz
1,807 Views

Ok, I've found the problem.

The issue is with this code:
 

#include <iostream>
#include <stdio.h>

class ACLASS {
    public:
      static void execute_static_method() __attribute__ ((constructor)) {
        printf("execute_static_method()\n");
      }
};


int main () {
  std::cout << "main" << std::endl;
  return  0;
}

 

The real issue seems to be a warning that is not raised if you are using static methods, which is the following:

warning #470: qualified name is not allowed in member declaration

 

If you remove the class definition, the warning is displayed. 

#include <iostream>
#include <stdio.h>

// This raise the warning
void execute_fnc() __attribute__ ((constructor)) {
  printf("execute_fnc()\n");
}


class ACLASS {
  public:
    // This does not raise it
    static void execute_static_method() __attribute__ ((constructor)) {
      printf("execute_static_method()\n");
    }
};


int main () {
  std::cout << "main" << std::endl;
  return  0;
}

 

If you separate the definition and declaration that shows the warning, it does not display the warning at all, it executes the function, but it does not execute the static method. 

#include <iostream>
#include <stdio.h>

void execute_fnc() __attribute__ ((constructor));
void execute_fnc(){
  printf("execute_fnc()\n");
};

class ACLASS {
  public:
    static void execute_static_method() __attribute__ ((constructor)); {
      printf("execute_static_method()\n");
    }
};


int main () {
  std::cout << "main" << std::endl;
  return  0;
}

The execution output is as follows

> ./main
execute_fnc()
main

But if you split the definition and declaration of the static method it does work

 

#include <iostream>
#include <stdio.h>

void execute_fnc() __attribute__ ((constructor));
void execute_fnc(){
  printf("execute_fnc()\n");
};

class ACLASS {
  public:
    static void execute_static_method() __attribute__ ((constructor));

};

void ACLASS::execute_static_method() {
  printf("execute_static_method()\n");
}


int main () {
  std::cout << "Here" << std::endl;
  return  0;
}

Now I just need to figure out how to do this with my code. 

0 Kudos
RahulV_intel
Moderator
1,807 Views

Hi,

Great observation. Let us know if we can close this thread.

 

--Rahul

0 Kudos
Jose_M_Monsalve_Diaz
1,807 Views

Yes, I think there is an issue ticket to be open here, but as far as this thread goes that is it. 

Thanks for your quick response. 

0 Kudos
RahulV_intel
Moderator
1,807 Views

Hi,

Thanks for the confirmation. We will go ahead and close this thread. Feel free to reach out to us, in case of further queries.

 

--Rahul

0 Kudos
Reply