Where is _libc_init_array() defined?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Where is _libc_init_array() defined?

Jump to solution
1,652 Views
oscar_abn
Contributor I

Hello, 

I am working on the S32K344 microcontroller and I need to execute c++ code. After finding out c++ is not officially suported with and SDK project I tried to implement some of the workarounds explained in nxp community pages. 

In short, the steps are: create a C project with SDK, add C++ nature, rename main.c to main.cpp, change standard linker file to the linker file created in the project in project settings (see image).

Screenshot 2024-09-13 134701.png

 

 

 

After this steps I am able to build a project with main.cpp, however the objects declared outside of main are not executed or initialized. 

One solution i found to this problem is to include  __libc_init_array() at the beginning of main(). The problem now is that this function is not found. How can i add the library to solve this dependence? Is there any other solution to initialize objects outside of main in c++? Console output:

Building file: ../RTD/src/Clock_Ip_ExtOsc.c
Invoking: Standard S32DS C Compiler
arm-none-eabi-gcc "@RTD/src/Adc_Sar_Ip.args" -MMD -MP -MF"RTD/src/Clock_Ip_ExtOsc.d" -MT"RTD/src/Clock_Ip_ExtOsc.o" -o "RTD/src/Clock_Ip_ExtOsc.o" "../RTD/src/Clock_Ip_ExtOsc.c"
../src/main.cpp: In function 'int main()':
../src/main.cpp:59:2: error: '__libc_init_array' was not declared in this scope
59 | __libc_init_array();
| ^~~~~~~~~~~~~~~~~
make: *** [src/subdir.mk:20: src/main.o] Error 1
make: *** Waiting for unfinished jobs....

 

0 Kudos
Reply
1 Solution
1,588 Views
jiri_kral
NXP Employee
NXP Employee

Hi, 

_libc_init_array is part of STD C library (for example libc_nano.a). If you are using C++ you don't need link STD C Library and call _libc_init_array. 

View solution in original post

0 Kudos
Reply
1 Reply
1,589 Views
jiri_kral
NXP Employee
NXP Employee

Hi, 

_libc_init_array is part of STD C library (for example libc_nano.a). If you are using C++ you don't need link STD C Library and call _libc_init_array. 

0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-1954260%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EWhere%20is%20_libc_init_array()%20defined%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1954260%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20working%20on%20the%20S32K344%20microcontroller%20and%20I%20need%20to%20execute%20c%2B%2B%20code.%20After%20finding%20out%20c%2B%2B%20is%20not%20officially%20suported%20with%20and%20SDK%20project%20I%20tried%20to%20implement%20some%20of%20the%20workarounds%20explained%20in%20nxp%20community%20pages.%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20short%2C%20the%20steps%20are%3A%20create%20a%20C%20project%20with%20SDK%2C%20add%20C%2B%2B%20nature%2C%20rename%20main.c%20to%20main.cpp%2C%20change%20standard%20linker%20file%20to%20the%20linker%20file%20created%20in%20the%20project%20in%20project%20settings%20(see%20image).%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Screenshot%202024-09-13%20134701.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Screenshot%202024-09-13%20134701.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F299057i2AD1C0A8374827FD%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Screenshot%202024-09-13%20134701.png%22%20alt%3D%22Screenshot%202024-09-13%20134701.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CP%3EAfter%20this%20steps%20I%20am%20able%20to%20build%20a%20project%20with%20main.cpp%2C%20however%20the%20objects%20declared%20outside%20of%20main%20are%20not%20executed%20or%20initialized.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EOne%20solution%20i%20found%20to%20this%20problem%20is%20to%20include%26nbsp%3B%20__libc_init_array()%20at%20the%20beginning%20of%20main().%20The%20problem%20now%20is%20that%20this%20function%20is%20not%20found.%20How%20can%20i%20add%20the%20library%20to%20solve%20this%20dependence%3F%20Is%20there%20any%20other%20solution%20to%20initialize%20objects%20outside%20of%20main%20in%20c%2B%2B%3F%20Console%20output%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EBuilding%20file%3A%20..%2FRTD%2Fsrc%2FClock_Ip_ExtOsc.c%3CBR%20%2F%3EInvoking%3A%20Standard%20S32DS%20C%20Compiler%3CBR%20%2F%3Earm-none-eabi-gcc%20%22%40RTD%2Fsrc%2FAdc_Sar_Ip.args%22%20-MMD%20-MP%20-MF%22RTD%2Fsrc%2FClock_Ip_ExtOsc.d%22%20-MT%22RTD%2Fsrc%2FClock_Ip_ExtOsc.o%22%20-o%20%22RTD%2Fsrc%2FClock_Ip_ExtOsc.o%22%20%22..%2FRTD%2Fsrc%2FClock_Ip_ExtOsc.c%22%3CBR%20%2F%3E..%2Fsrc%2Fmain.cpp%3A%20In%20function%20'int%20main()'%3A%3CBR%20%2F%3E..%2Fsrc%2Fmain.cpp%3A59%3A2%3A%20error%3A%20'__libc_init_array'%20was%20not%20declared%20in%20this%20scope%3CBR%20%2F%3E59%20%7C%20__libc_init_array()%3B%3CBR%20%2F%3E%7C%20%5E~~~~~~~~~~~~~~~~%3CBR%20%2F%3Emake%3A%20***%20%5Bsrc%2Fsubdir.mk%3A20%3A%20src%2Fmain.o%5D%20Error%201%3CBR%20%2F%3Emake%3A%20***%20Waiting%20for%20unfinished%20jobs....%3C%2FP%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1956507%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Where%20is%20_libc_init_array()%20defined%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1956507%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%2C%26nbsp%3B%3C%2FP%3E%0A%3CP%3E_libc_init_array%20is%20part%20of%20STD%20C%20library%20(for%20example%20libc_nano.a).%20If%20you%20are%20using%20C%2B%2B%20you%20don't%20need%20link%20STD%20C%20Library%20and%20call%20_libc_init_array.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E