DarkHelp icon indicating copy to clipboard operation
DarkHelp copied to clipboard

'undefined reference to function' after -static compilation using make for DarkHelp project

Open ashishmagar600 opened this issue 3 years ago • 3 comments

OS: Ubuntu 18.04 LTS

Built DarkNet succesfully and can be used with './darknet'

Built DarkHelp using this and compiled 'example_project'.

I am trying to build a standalone executable "exe_static" to be used on linux systems, where I might not have the privileges to install dependencies.

Copied DarkHelp/build/src-lib/libdarkhelp.a and libdarknet.a to /usr/lib/

exe_static.cpp

#include <DarkHelp.hpp>
#include <typeinfo>


int main(int argc, char *argv[])
{
	DarkHelp::Config cfg("model.cfg", "model.weights", "names.list");
	cfg.enable_tiles			= true;
	cfg.combine_tile_predictions		= true;
	cfg.annotation_auto_hide_labels		= false;
	cfg.annotation_include_duration		= false;
	cfg.annotation_include_timestamp	= false;

	DarkHelp::NN nn(cfg);
	const auto results = nn.predict(argv[1]);

//        std::cout <<"Shape of result = : " << results.size() << "\n";

	return 0;
}

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED (VERSION 3.0)

PROJECT (ExampleProject C CXX)

SET (CMAKE_BUILD_TYPE Release)
SET (CMAKE_CXX_STANDARD 17)
SET (CMAKE_CXX_STANDARD_REQUIRED ON)

ADD_DEFINITIONS ("-Wall -Wextra -Werror -Wno-unused-parameter ")

FIND_PACKAGE (Threads	REQUIRED)
FIND_PACKAGE (OpenCV	REQUIRED)
#FIND_LIBRARY (DARKHELP	libdarkhelp.a)
#FIND_LIBRARY (DARKNET	libdarknet.a)

INCLUDE_DIRECTORIES (${OpenCV_INCLUDE_DIRS})

FILE (GLOB SOURCE *.cpp)
LIST (SORT SOURCE)

SET (BUILD_SHARED_LIBS=OFF)
SET (OPENCV_GENERATE_PKGCONFIG=YES)


ADD_EXECUTABLE (exe_static ${SOURCE})

SET_TARGET_PROPERTIES(exe_static PROPERTIES 
   LINK_SEARCH_START_STATIC ON
   LINK_SEARCH_END_STATIC ON
)

set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")

set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

#TARGET_LINK_LIBRARIES (exe_static Threads::Threads ${DARKHELP} ${DARKNET} ${OpenCV_LIBS})
TARGET_LINK_LIBRARIES (exe_static Threads::Threads libdarknet.a libdarkhelp.a  ${OpenCV_LIBS} )

Same file structure as DarkHelp/example_project

$> cd build $> cmake ..

-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Found OpenCV: /usr/local (found version "3.4.4") 
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/exe_static/build

$> make

Scanning dependencies of target exe_static
[ 50%] Building CXX object CMakeFiles/exe_static.dir/exe_static.cpp.o
[100%] Linking CXX executable exe_static
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libdarkhelp.a(DarkHelpNN.cpp.o): In function `DarkHelp::NN::reset()':
DarkHelpNN.cpp:(.text+0x1ff8): undefined reference to `free_network'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libdarkhelp.a(DarkHelpNN.cpp.o): In function `DarkHelp::NN::predict_internal_darknet()':
DarkHelpNN.cpp:(.text+0x45ca): undefined reference to `make_image'
DarkHelpNN.cpp:(.text+0x4715): undefined reference to `network_predict'
DarkHelpNN.cpp:(.text+0x4761): undefined reference to `get_network_boxes'
DarkHelpNN.cpp:(.text+0x4b7e): undefined reference to `free_detections'
DarkHelpNN.cpp:(.text+0x4ba7): undefined reference to `free_image'
DarkHelpNN.cpp:(.text+0x5190): undefined reference to `do_nms_sort'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libdarkhelp.a(DarkHelpNN.cpp.o): In function `DarkHelp::NN::init()':
DarkHelpNN.cpp:(.text+0xaa81): undefined reference to `load_network_custom'
DarkHelpNN.cpp:(.text+0xaaa7): undefined reference to `calculate_binary_weights'
collect2: error: ld returned 1 exit status
CMakeFiles/exe_static.dir/build.make:139: recipe for target 'exe_static' failed
make[2]: *** [exe_static] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/exe_static.dir/all' failed
make[1]: *** [CMakeFiles/exe_static.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2


ashishmagar600 avatar May 25 '22 07:05 ashishmagar600

All of those functions are Darknet functions. So you're not linking correctly to Darknet. Or maybe you need to specify Darknet after DarkHelp when linking?

stephanecharette avatar May 25 '22 09:05 stephanecharette

I realized that the linker may be throwing away some unused functions so I had tried this:

TARGET_LINK_LIBRARIES (exe_static libdarkhelp.a libdarknet.a ${OpenCV_LIBS} Threads::Threads )

But still similar error:

$> make

Scanning dependencies of target exe_static
[ 50%] Building CXX object CMakeFiles/exe_static.dir/exe_static.cpp.o
[100%] Linking CXX executable exe_static
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libdarkhelp.a(DarkHelpNN.cpp.o): In function `DarkHelp::NN::init()':
DarkHelpNN.cpp:(.text+0xaa81): undefined reference to `load_network_custom'
DarkHelpNN.cpp:(.text+0xaaa7): undefined reference to `calculate_binary_weights'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libpthread.a(pthread_create.o): In function `allocate_stack':
/build/glibc-uZu3wS/glibc-2.27/nptl/allocatestack.c:526: undefined reference to `_dl_stack_flags'
/build/glibc-uZu3wS/glibc-2.27/nptl/allocatestack.c:652: undefined reference to `_dl_stack_flags'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libpthread.a(elision-lock.o): In function `do_set_elision_enable':
/build/glibc-uZu3wS/glibc-2.27/nptl/../sysdeps/unix/sysv/linux/x86/elision-conf.c:67: undefined reference to `_dl_x86_cpu_features'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libpthread.a(nptl-init.o): In function `__pthread_initialize_minimal_internal':
/build/glibc-uZu3wS/glibc-2.27/nptl/nptl-init.c:294: undefined reference to `_dl_cpuclock_offset'
/build/glibc-uZu3wS/glibc-2.27/nptl/nptl-init.c:429: undefined reference to `_dl_pagesize'
/build/glibc-uZu3wS/glibc-2.27/nptl/nptl-init.c:438: undefined reference to `_dl_pagesize'
/build/glibc-uZu3wS/glibc-2.27/nptl/nptl-init.c:454: undefined reference to `_dl_init_static_tls'
/build/glibc-uZu3wS/glibc-2.27/nptl/nptl-init.c:456: undefined reference to `_dl_wait_lookup_done'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libpthread.a(nptl-init.o): In function `__pthread_get_minstack':
/build/glibc-uZu3wS/glibc-2.27/nptl/nptl-init.c:475: undefined reference to `_dl_pagesize'
collect2: error: ld returned 1 exit status
CMakeFiles/exe_static.dir/build.make:139: recipe for target 'exe_static' failed
make[2]: *** [exe_static] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/exe_static.dir/all' failed
make[1]: *** [CMakeFiles/exe_static.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

ashishmagar600 avatar May 25 '22 09:05 ashishmagar600

No updates. Can this be closed?

stephanecharette avatar Aug 31 '22 19:08 stephanecharette