1

I have a project that uses cmake as the build system and I have a problem with linking.

I have haeader file called lwm2mclient.h that has the following struct and pointer declarations.

typedef struct
{
    char binary_filename[256];
}programming;

extern programming device1;
extern programming device2;
extern programming *programmingPtr1;
extern programming *programmingPtr2;

I have a souce files that includes lwm2mclient.h which assigns the pointers to the instances liek this.

programming *programmingPtr1 = &device1;
programming *programmingPtr2 = &device2;

But I get an error bulding

dut_object.c.o:(.data+0x0): undefined reference todevice1'`

I gather this is related to incorrect linking but I can't understand whats wrong in the cmakelists.txt file.

Thi is the cmakelists.txt file and both files are set as sources.

Appreciate it if somebody could review it please?

cmake_minimum_required (VERSION 3.0)

project (lwm2mclient)

option(DTLS "Enable DTLS" OFF)

# Compiler include
#INC1 = ~/Documents/wakaama-master-2_0/examples/IniParser/iniparser-master/src

SET(CRADLE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../cradle-util/libcradle)

include(${CMAKE_CURRENT_LIST_DIR}/../../core/wakaama.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../shared/shared.cmake)

add_definitions(-DLWM2M_CLIENT_MODE -DLWM2M_BOOTSTRAP -DLWM2M_SUPPORT_JSON)
add_definitions(${SHARED_DEFINITIONS} ${WAKAAMA_DEFINITIONS})

include_directories (${WAKAAMA_SOURCES_DIR} ${SHARED_INCLUDE_DIRS} ${CRADLE_INCLUDE_DIR} )

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -O0")

message("CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}")

SET(SOURCES
    lwm2mclient.c
    lwm2mclient.h
    system_api.c
    object_security.c
    object_server.c
    object_device.c
    object_firmware.c
    object_location.c
    object_connectivity_moni.c
    object_connectivity_stat.c
    object_access_control.c
    test_object.c
    dut_object.c
    usb_control.c
    )

find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB_PKG glib-2.0)
message(Found glib-2.0)
include_directories(${GLIB_PKG_INCLUDE_DIRS})


add_executable(${PROJECT_NAME} ${SOURCES} ${WAKAAMA_SOURCES} ${SHARED_SOURCES})

target_link_libraries(${PROJECT_NAME} cradle.a mraa.so m pthread iniparser.a )

LINK_DIRECTORIES(/usr/local/lib /usr/lib/x86_64-linux-gnu ${CMAKE_CURRENT_LIST_DIR}/../../../cradle-util/libcradle )


# Add WITH_LOGS to debug variant
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:WITH_LOGS>)
riverrock
  • 97
  • 2
  • 8
  • In which file do you declare `device1`? Are you not linking with it? – Chris Turner Jul 27 '17 at 16:02
  • linker just has not found the device1 in any of your object files – 0___________ Jul 27 '17 at 16:06
  • 2
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Chris Turner Jul 27 '17 at 16:37

0 Answers0