Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
7646 Discussions

Shared library constructors do not work when linking with -no-cxxlib

djungl
Novice
402 Views

Hello,
I have a simple shared library that defines a constructor and a destructor using __attribute__((constructor)) and __attribute__((destructor)). Everything is in C. This works fine unless I add -no-cxxlib to the linker command. When I do that then the shared library constructor/destructor are no longer invoked.

Is that intended behavior? Is there a way I can keep them working although I have -no-cxxlib on the command line?

My shared library source code is

 

#include <stdio.h>

__attribute__((constructor)) void ctor(void) {
  printf("Here is the constructor\n");
}

__attribute__((destructor)) void dtor(void) {
  printf("Here is the destructor\n");
}

void function(void) {
  printf("Here is the function\n");
}

 

and my application source code is

 

void function(void);
int main(void) {
  function();
  return 0;
}

 

I use this makefile:

 

ICC = icc
NO_CXXLIB = -no-cxxlib
all: main libsolib.so
	LD_LIBRARY_PATH=. ./main

main.o: main.c
	$(ICC) -O3 -c -o $@ $<

main: main.o libsolib.so
	$(ICC) -o $@ main.c -L. -lsolib

solib.o: solib.c
	$(ICC) -W -Wall -fPIC -c -o $@ $<
libsolib.so: solib.o
	$(ICC) -o $@ -O3 -shared -static-intel $(NO_CXXLIB) $<

clean:
	rm -f *.so *.o

 

Running "make all" (unexpectedly) prints

Here is the function

while running "make NO_CXXLIB= all" prints (constructor and destructor are invoked)

Here is the constructor
Here is the function
Here is the destructor

It seems that -no-cxxlib not only prevents linking of gcc C++ code but also library startup code?

I am on Linux and this the compiler version I am using:

icc (ICC) 18.0.5 20180823

0 Kudos
1 Solution
MRajesh_intel
Moderator
368 Views

Hi,

 

We tried running it on icc (ICC) 2021.2.0 version, both the constructor and the destructor got invoked for both the cases(fig. attached below).

 

Please try upgrading to latest version icc by installing latest oneAPI Toolkit.

Link:https://software.intel.com/content/www/us/en/develop/tools/oneapi/base-toolkit/download.html

 

Regards

Rajesh.

 

View solution in original post

5 Replies
MRajesh_intel
Moderator
369 Views

Hi,

 

We tried running it on icc (ICC) 2021.2.0 version, both the constructor and the destructor got invoked for both the cases(fig. attached below).

 

Please try upgrading to latest version icc by installing latest oneAPI Toolkit.

Link:https://software.intel.com/content/www/us/en/develop/tools/oneapi/base-toolkit/download.html

 

Regards

Rajesh.

 

djungl
Novice
339 Views

Thanks for checking. Unfortunately, upgrading my Intel compiler is not an option for me.

Given what you say, I conclude that this is unexpected behavior and probably a bug in my current version. I will try to work around this then.

 

Regards

Daniel

MRajesh_intel
Moderator
320 Views

Hi,


We do not support ICC 18.0 anymore. So, can we proceed to close this thread?


Regards

Rajesh.


djungl
Novice
312 Views

Yes, sure. Sorry for not doing that before. I accepted your initial answer now.

MRajesh_intel
Moderator
307 Views

Thanks for the confirmation!


As this issue has been resolved, we will no longer respond to this thread. If you require any additional assistance from Intel, please start a new thread. Any further interaction in this thread will be considered community only.

Have a Good day.

 

Regards

Rajesh.


Reply