undefined reference to function in Eclipse CDT
Asked Answered
R

2

6

In my main.c program I include this library:

#include "coap_api.h"

That is also included in Properties -> C/C++ Build -> Settings -> Cross ARM GNU Assembler -> Includes

and

Properties -> C/C++ Build -> Settings -> Cross ARM C Compiler -> Includes

Building the project I got this error:

./src/main.o: In function `main':
/Users/joanet/Development/Embedded/eclipseWS/EHAL-master/ARM/Nordic/nRF51/ipv6_coap_server/Release/../src/main.c:708: undefined reference to `coap_init' 

I read in another post that I have to link the library (http://whatwouldnickdo.com/wordpress/328/eclipse-cdt-and-linux-libraries/)

I've tried to add the library with the name coap_api but I does not work

lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/bin/ld: cannot find -lcoap_api

in the compilation the coap directory is included:

arm-none-eabi-gcc -mcpu=cortex-m0 -mthumb -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall  -g -DBLE_STACK_SUPPORT_REQD -DNRF51 -D__CORTEX_M0 -DBOARD_PCA10028 -DSOFTDEVICE_PRESENT -DS110 -I/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/ipv6_coap_server/bsp -I/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/iot/ipv6_stack/pbuffer -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/util/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/drivers_nrf/hal/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/../../../ARM/CMSIS/CMSIS/Include/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/../../../ARM/Nordic/nRF51/CMSIS/include/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/softdevice/s110/headers/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/common/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/ble_services/ble_bas/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/ble_services/ble_dis/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/device_manager/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/softdevice/common/softdevice_handler/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/scheduler/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/ble_error_log/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/drivers_nrf/ble_flash/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/ble_debug_assert_handler/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/timer/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/gpiote/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/drivers_nrf/pstorage/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/ble_services/ble_dfu/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/bootloader_dfu/experimental/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/bootloader_dfu/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/../../../include/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/ble_services/ble_hids/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/../../../ARM/CMSIS/CMSIS_RTX/INC/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/trace/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/device/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/ble_services/ble_tps/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/button/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/device_manager/config/" -I"/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/ipv6_coap_client/src" -I/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/toolchain/gcc -I/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/libraries/mem_manager -I/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/ble/ble_services/ble_ipsp -I/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/iot/include -I/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/iot/ipv6_stack/include -I/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/nrf51_sdk/components/iot/coap -std=gnu99 -MMD -MP -MF"src/Vectors_nRF51.d" -MT"src/Vectors_nRF51.o" -c -o "src/Vectors_nRF51.o" "/Users/nullpointer/Development/Embedded/eclipseWS/EHAL-master-Hoan/ARM/Nordic/nRF51/src/Vectors_nRF51.c"
Rumney answered 9/5, 2015 at 18:28 Comment(3)
The error is generated by the GNU linker; EclipseCDT has little to do with it. Post a complete build log showing the command lines being executes not just the errors.Exact
where I can see the build logs ? in the console ?Chiliarch
I have no idea; I don't use Eclipse; but it must surely be available somewhere - probably the entire text from wherever you are reading these errors. Context is useful and better for use to see what the build is doing rather then you describe it.Exact
E
4

The line:

#include "coap_api.h"

does not include a library, it includes a header file containing declarations defined elsewhere. You do have to link the separately compiled object code containing those definitions.

If that is a library file, you need to set the library path in a similar manner to the include paths. If the library has a name of the GNU conventional form libXXX.a, then it is linked with the switch -lXXX, otherwise you must specify the complete file name.

If however the declarations in coap_api.h refer to definitions in source code form, then you must add the source to your project for separate compilation and linking.

Exact answered 10/5, 2015 at 21:17 Comment(4)
It's inside another c program (coap.c) that is included in Properties -> C/C++ Build -> Settings -> Cross ARM GNU Assembler -> Includes and Properties -> C/C++ Build -> Settings -> Cross ARM C Compiler -> IncludesChiliarch
That is not "another program", it is a separate translation unit in your program. It should not be "included", it should be compiled an linked. The project include settings should specify only directory paths, not actual files.Exact
Yes, it includes the directory where is located coap.c and coap_api.hChiliarch
The libraries path is used to search for .a (archive) files, and I think also .o (object files), it will have no effect pointing it at a ,c file; you have to add it to your project for compilation alongside main.c in this case, or build a library separately. The last paragraph of my answer therefore applies. This is where the complete build log would have been a useful diagnostic.Exact
I
1

You need to link generate object files from coap.c then either make a library from this .o or directly compile your project with this previously generated coap.o file.

You should read some documentation about Compiling and Linking. Here your compiler tells you that it has undefined reference to coap_init, which is effectively beeing called by your program and referenced by the coap_api.h file. The problem is that it cannot find the associated machine code. You may have included the coap.c directory, it's nothing more than a text file. You first need to compile it and then create a library which you link to the project or copy import the coap.c in your project so that it gets compiled with the rest of your work.

Idette answered 19/5, 2015 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.