undefined reference to function in Eclipse CDT

Nunyet de Can Calçada picture Nunyet de Can Calçada · May 9, 2015 · Viewed 9.1k times · Source

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"

Answer

Clifford picture Clifford · May 10, 2015

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.