CMake does not properly find CUDA library - c++

I'm trying to build a program that requires CUDA. To the CMake script I supply:
cmake -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda ..
CUDA is found and CMake runs normally:
staudt ~/workspace/clutbb/cluster/build $ cmake -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda ..
-- Found CUDA: /usr/local/cuda (found version "6.5")
-- Found Intel TBB
-- Boost version: 1.56.0
-- Found the following Boost libraries:
-- iostreams
-- program_options
-- Looking for include file pthread.h
-- Looking for include file 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
-- Could NOT find SDL (missing: SDL_LIBRARY SDL_INCLUDE_DIR)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/i11/staudt/workspace/clutbb/cluster/build
But then the linker step fails:
staudt ~/workspace/clutbb/cluster/build $ make
[ 69%] Built target cluster
Linking CXX executable clu
CMakeFiles/clu.dir/clu.cpp.o: In function `initCUDA(int&, CUctx_st*&, int const&)':
clu.cpp:(.text+0x517): undefined reference to `cuInit'
clu.cpp:(.text+0x52b): undefined reference to `cuDeviceGet'
clu.cpp:(.text+0x53f): undefined reference to `cuCtxCreate_v2'
clu.cpp:(.text+0x559): undefined reference to `cuDeviceGetName'
clu.cpp:(.text+0x55e): undefined reference to `cuCtxSynchronize'
CMakeFiles/clu.dir/clu.cpp.o: In function `exitCUDA(int&, CUctx_st*&)':
clu.cpp:(.text+0x684): undefined reference to `cuCtxDestroy_v2'
CMakeFiles/clu.dir/clu.cpp.o: In function `main':
clu.cpp:(.text.startup+0x1092): undefined reference to `cuCtxDestroy_v2'
clu.cpp:(.text.startup+0x10d1): undefined reference to `cuCtxSynchronize'
clu.cpp:(.text.startup+0x10e1): undefined reference to `cuCtxSynchronize'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/clu] Fehler 1
make[1]: *** [bin/CMakeFiles/clu.dir/all] Fehler 2
make: *** [all] Fehler 2
The required library is at /usr/local/cuda/lib64/stubs/libcuda.so, but how can I point that out to cmake or make?

In the archive you have now posted, there are multiple project hierarchies. The actual error you have posted in the question is occurring during compile and linking of the clu project based on clu.cpp in the clutbb/cluster/bin directory.
In this same directory, there is a CMakeLists.txt file. This file governs this particular level of the project hierarchy.
In this particular CMakeLists.txt file, there is the following section:
cuda_add_executable(clu clu.cpp)
target_link_libraries(clu ${CUDA_LIBRARY} ${TBB_LIBRARY} ${Boost_LIBRARIES} rt)
target_link_libraries(clu cluster)
Try modifying the middle line above to:
target_link_libraries(clu ${CUDA_LIBRARY} ${TBB_LIBRARY} ${Boost_LIBRARIES} rt cuda)
This should fix the missing -lcuda in the linker command line. It may still be necessary to give it the path to libcuda.so on your machine, but it may not be necessary, depending on how your machine environment is set up.

The correct way of doing this on CMake 3.17+ is to use the FindCUDAToolkit module, like so:
find_package(CUDAToolkit REQUIRED)
target_link_libraries(my_target PRIVATE CUDA::cudart CUDA::cuda_driver)
The CUDA::cuda_driver target is equivalent to -lcuda when the linker would find it, and is otherwise an absolute path to the correct library. You should avoid adding system libraries via target_link_libraries to ensure portability.
If you have an older version (CMake 3.0+), you can use the (now-deprecated) FindCUDA module to imitate the 3.17 module, like so:
find_package(CUDA REQUIRED)
# Do what the new package does
find_library(CUDA_DRIVER_LIBRARY
NAMES cuda_driver cuda
HINTS ${CUDA_TOOLKIT_ROOT_DIR}
ENV CUDA_PATH
PATH_SUFFIXES nvidia/current lib64 lib/x64 lib)
if (NOT CUDA_DRIVER_LIBRARY)
# Don't try any stub directories until we have exhausted all other search locations.
find_library(CUDA_DRIVER_LIBRARY
NAMES cuda_driver cuda
HINTS ${CUDA_TOOLKIT_ROOT_DIR}
ENV CUDA_PATH
PATH_SUFFIXES lib64/stubs lib/x64/stubs lib/stubs stubs)
endif ()
mark_as_advanced(CUDA_DRIVER_LIBRARY)
##
target_include_directories(my_target PRIVATE ${CUDA_INCLUDE_DIRS})
target_link_libraries(my_target PRIVATE ${CUDA_LIBRARIES} ${CUDA_DRIVER_LIBRARY})
This is adapted from the official sources, here.

The C++ file with host calls doesnt know it needs to link to libcudart. You have to explicitly set dependencies for the file/binary that file is in, eg.
target_link_libraries(clu ${CUDA_LIBRARIES})
The above response is slightly wrong. It looks like libcuda.so is installed in unexpected location for whatever reason. You can try setting CMAKE_LIBRARY_PATH or/and CUDA_LIB_PATH to that path.
The CUDA_LIB_PATH needs to be set outside cmake I think, eg export CUDA_LIB_PATH=/usr/local/cuda/lib64/stubs/

Related

C++ Tensorflow Lite undefined referance

I'm trying to build a project using Tensorflow Lite on my debian 11 machine, however it says I'm getting an undefined reference on some functions.
Here is the code I'm trying to run:
// Works
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile(filename);
TFLITE_MINIMAL_CHECK(model != nullptr);
// Undefined referance:
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
The first few lines in on itself works fine. When I add the lines below starting from BuiltinOpResolver I'm getting the following error when running make:
[ 50%] Linking CXX executable TFLiteCheck
/usr/bin/ld: CMakeFiles/TFLiteCheck.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x106): undefined reference to `tflite::InterpreterBuilder::InterpreterBuilder(tflite::FlatBufferModel const&, tflite::OpResolver const&)'
/usr/bin/ld: main.cpp:(.text+0x11f): undefined reference to `tflite::InterpreterBuilder::operator()(std::unique_ptr<tflite::Interpreter, std::default_delete<tflite::Interpreter> >*)'
/usr/bin/ld: main.cpp:(.text+0x12e): undefined reference to `tflite::InterpreterBuilder::~InterpreterBuilder()'
/usr/bin/ld: main.cpp:(.text+0x19e): undefined reference to `tflite::InterpreterBuilder::~InterpreterBuilder()'
/usr/bin/ld: CMakeFiles/TFLiteCheck.dir/main.cpp.o: in function `std::default_delete<tflite::Interpreter>::operator()(tflite::Interpreter*) const':
main.cpp:(.text._ZNKSt14default_deleteIN6tflite11InterpreterEEclEPS1_[_ZNKSt14default_deleteIN6tflite11InterpreterEEclEPS1_]+0x1e): undefined reference to `tflite::Interpreter::~Interpreter()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/TFLiteCheck.dir/build.make:104: TFLiteCheck] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/TFLiteCheck.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
I've tried [this answer][1] but it's on an arm architecture while I'm on an intel chip, and when I try it regardless I'm getting a completely different error that I've never seen before.
I've followed these steps to setup TFLite:
Got the source code from tenserflow github page
Got bazel version 3.7.2 (bazel-3.7.2-linux-x86_64)
Ran python3 ./configure.py setting everything to default and opting to say n to everything
Ran bazel build -c opt //tensorflow/lite:libtensorflowlite.so --local_ram_resources=10240 --config=noaws(Tried it with and without --local_ram_resources=10240 --config=noaws params)
Move the .so file to the designated file, right next to tenserflow include files.
Ran cmake .. and make on the build folder with the following CMake file:
cmake_minimum_required(VERSION 3.17)
project(TFLiteCheck)
set(CMAKE_CXX_STANDARD 14)
# include has 2 subdirectories: tensorflow and flatbuffers
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/third-party/tflite-dist/include/)
# lib has 1 file: libtensorflowlite.so
ADD_LIBRARY(tensorflowlite SHARED IMPORTED)
set_property(TARGET tensorflowlite PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/third-party/tflite-dist/libs/linux_x64/libtensorflowlite.so)
add_executable(TFLiteCheck main.cpp)
target_link_libraries(TFLiteCheck PUBLIC tensorflowlite)
And running make results in the above error. What could be the problem? Is there a better way to setup tenserflow? Like I've said only running FlatBufferModel works just fine.
Update:
By removing the -J flag from the official build instructions I've managed to built the project propperly. However when I use the official cmake example:
cmake_minimum_required(VERSION 3.16)
project(minimal C CXX)
set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
"Directory that contains the TensorFlow project" )
if(NOT TENSORFLOW_SOURCE_DIR)
get_filename_component(TENSORFLOW_SOURCE_DIR
"${CMAKE_CURRENT_LIST_DIR}/../../../../" ABSOLUTE)
endif()
add_subdirectory(
"${TENSORFLOW_SOURCE_DIR}/user/tensorflow_src/tensorflow/lite"
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)
add_executable(minimal main.cpp)
target_link_libraries(minimal tensorflow-lite)
When I run this with my example main.cpp using cmake . provided above, I'm getting this output and the terminal is stuck like this, without resolving:
user#debian:~/Desktop/SmartAlpha/tf_test$ cmake .
-- Setting build type to Release, for debug builds use'-DCMAKE_BUILD_TYPE=Debug'.
CMake Warning at abseil-cpp/CMakeLists.txt:70 (message):
A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
3.8 and up. We recommend enabling this option to ensure your project still
builds correctly.
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- 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
It's not frozen or anything, it just stays like this untill I hit ctrl+c to break it, without ever finishing.
Update 2:
The compilation finished with this error:
user#debian:~/Desktop/SmartAlpha/tf_test$ cmake .
-- Setting build type to Release, for debug builds use'-DCMAKE_BUILD_TYPE=Debug'.
CMake Warning at abseil-cpp/CMakeLists.txt:70 (message):
A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
3.8 and up. We recommend enabling this option to ensure your project still
builds correctly.
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- 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
CMake Error at eigen/CMakeLists.txt:36 (message):
In-source builds not allowed. Please make a new directory (called a build
directory) and run CMake from there. You may need to remove
CMakeCache.txt.
-- Configuring incomplete, errors occurred!
See also "/home/user/Desktop/tf_test/CMakeFiles/CMakeOutput.log".
See also "/home/user/Desktop/tf_test/CMakeFiles/CMakeError.log".
Which cmake cache is it talking about? The one in my projects directory or the one inside my tensorflow build?
Am I missing spmething?
[1]: Tensorflow Lite error undefined reference to `tflite::DefaultErrorReporter()'

Linking a prebuilt binary in cpp does not work?

I just build the following package: faiss and my cpp program now recognises all library header so I can include them into my program. But my programm throws undefined reference to. So when I built faiss it created a libfaiss.a and a libfaiss.so which one to link and how??? I think I have tried any solution that I could find in the internet, but my knowledge of the build/linking process is limited.
My Faiss build-directory looks like this:
faiss-1.5.3/
-libfaiss.a
-libfaiss.so
-foo.h
-foo.o
-foo.cpp
-bar.h
-...
My tries. (I am using ROS, catkin_simple and gtests)
So this is how I would usually do it:
find_library(LibFaiss faiss HINT /home/tim/faiss/faiss-1_5_3/faiss-1.5.3/)
catkin_add_gtest(test_inverted_nn test/test_inverted-nn.cc
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}})
target_link_libraries(test_inverted_nn ${LIBRARY_NAME} ${LibFaiss})
I get the following Error:
/usr/bin/ld: cannot find -lLibFaiss-NOTFOUND}
collect2: error: ld returned 1 exit status
CMakeFiles/test_inverted_nn.dir/build.make:358: recipe for target '/home/tim/catkin_ws/devel/lib/nn/test_inverted_nn' failed
make[3]: *** [/home/tim/catkin_ws/devel/lib/inverted_nn/test_inverted_nn] Error 1
I also found this Solution here Import an external library into a ROS node, but that does not work for me:
add_library(libfaiss STATIC IMPORTED)
set_target_properties(libfaiss PROPERTIES IMPORTED_LOCATION /home/tim/faiss/faiss-1_5_3/faiss-1.5.3/libfaiss.a)
target_link_libraries(test_inverted_nn ${LIBRARY_NAME} ${libfaiss})
This throws the following Error:
/usr/bin/ld: cannot find -l}
collect2: error: ld returned 1 exit status
EDIT
After correcting the not_found error I am getting the following error:
CMake Warning at /opt/ros/melodic/share/catkin/cmake/test/gtest.cmake:180 (add_executable):
Cannot generate a safe runtime search path for target
test_inverted_multi_index because files in some directories may conflict
with libraries in implicit directories:
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/usr/local/lib
Some of these libraries may not be found correctly.
*** It does not matter what I add inside target_link_libraries I am always getting that error***
EDIT2
So with the tipp of #Aconcagua I deleted everything from the CMakeLists.txt and only added the following to my cmake options:
-DCMAKE_CXX_FLAGS=-L/home/tim/faiss/faiss-1_5_3/faiss-1.5.3/
But Im still getting the undefined reference errors. It does not make any difference

Cmake error undefined reference to `pthread_create'

I make a test for cmake FindThreads. Here is my source code test.cpp and CMakeLists.txt:
#include <pthread.h>
void* test_func(void* data)
{
return data;
}
int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, test_func, NULL);
pthread_detach(thread);
pthread_cancel(thread);
pthread_join(thread, NULL);
pthread_atfork(NULL, NULL, NULL);
pthread_exit(NULL);
return 0;
}
cmake_minimum_required(VERSION 3.5)
project(test C CXX)
set(CMAKE_THREAD_PREFER_PTHREAD ON)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_executable(test test.cpp)
if(TARGET Threads::Threads)
target_link_libraries(test PRIVATE Threads::Threads)
endif()
when I run:
cmake .
I get the outpu:
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
then I check CMakeError.txt, find that:
gmake[1]: Entering directory '/home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_55ab6.dir/src.c.o
/usr/bin/clang -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_55ab6.dir/src.c.o -c /home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_55ab6
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_55ab6.dir/link.txt --verbose=1
/usr/bin/clang -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_55ab6.dir/src.c.o -o cmTC_55ab6
/usr/bin/ld: CMakeFiles/cmTC_55ab6.dir/src.c.o: in function `main':
src.c:(.text+0x35): undefined reference to `pthread_create'
/usr/bin/ld: src.c:(.text+0x41): undefined reference to `pthread_detach'
/usr/bin/ld: src.c:(.text+0x4d): undefined reference to `pthread_cancel'
/usr/bin/ld: src.c:(.text+0x5f): undefined reference to `pthread_join'
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[1]: *** [CMakeFiles/cmTC_55ab6.dir/build.make:107: cmTC_55ab6] Error 1
gmake[1]: Leaving directory '/home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:141: cmTC_55ab6/fast] Error 2
My question is why performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed, and since it failed, did it
really find Threads, I am totally confused. Thanks for any replying!
There is a little reason to examine CMakeError.txt until CMake reports about not-working compiler or CMake reports about unavailable feature, which is detected by probe compilation/linking, but you expect that feature to be available.
In your case you have successful CMake configuration (look at the last lines in CMake output), and you have successfully detected Threads-related library(see below). No reasons to worry and no reasons to look into CMakeError.txt.
did it really find Threads?
Yes, Threads are found. E.g., CMake clearly states
-- Found Threads: TRUE
Other ways for deduce, that Threads has been found:
You use REQUIRED keyword with find_package(Threads). Would Threads not found, CMake will report about an error and terminate configuration.
You may check Threads_FOUND variable after the find_package(Threads) call. (With REQUIRED keyword this check is redudant).
You may check Threads::Threads target after the find_package(Threads) call. (With REQUIRED keyword this check is redudant).
FindThreads.cmake tries it best to determine if the compiler supports pthreads as a library, a compile time switch, or a link time switch, etcetera.
The failure you are seeing is if a pthreads library exists. It doesn't. Instead you are supposed to compile the files with -pthreads which is a compile time switch your compiler does accept. This is align with the clang documentation which shows that -pthreads is a compiler option, not a linker option. https://clang.llvm.org/docs/ClangCommandLineReference.html#compilation-flags
So, did it find Threads? Not exactly, there is nothing to find. Did it determine how to use pthreads? Yes.
You didn't show any errors when building the target test with test.cpp. You only posted errors one would expect when pthreads is not implemented as a separate library.

Cross-compiling enet from Linux to Windows, linking errors

Introduction
I'm trying to cross compile to Windows from Linux using MinGW-W64.
I had it working before I added enet to my project, however I'm now receiving issues with linking to enet
CMake finds enet correctly. ENET_LIBRARY and ENET_INCLUDE_DIR are set to the right locations.
ENet contains the symbols, as verified using /usr/x86_64-w64-mingw32/bin/objdump /usr/local/mingw64/lib/libenet.a -t
Build fails with "undefined reference to `enet_address_set_host'"
I'm able to compile the same code base natively using Visual Studio and VCPkg
Edit: Checking the contents of libenet.a verifies that it's a problem with cross-compiling enet, not my program in particular
None of my other dependencies use GNU autoconf, so I expect there's a problem there.
The Error
-- The C compiler identification is GNU 8.2.0
-- The CXX compiler identification is GNU 8.2.0
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc -- 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/x86_64-w64-mingw32-g++
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SFML 2.5 in /usr/local/mingw64/include
-- Found SFGUI in /usr/local/mingw64/include
-- Found Thor in /usr/local/mingw64/include
-- Found Lua in /usr/local/mingw64/include/lua5.1/
-- Found Lua: /usr/local/mingw64/lib/liblua5.1.a
-- Found ENet: /usr/local/mingw64/lib/libenet.a
-- Found ENet in /usr/local/mingw64/include
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE
/usr/local/mingw64/lib/liblua5.1.a/usr/local/mingw64/lib/libenet.awsock32ws2_32winmm
-- Adding executable: client (with server)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ruben/dev/rvwp
Scanning dependencies of target rvwp
[SNIP]
[ 98%] Building CXX object CMakeFiles/rvwp.dir/source/tests/t_chunk.cpp.obj
[100%] Linking CXX executable bin/rvwp.exe
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(address.cpp.obj):address.cpp:(.text+0xc6): undefined reference to `enet_address_set_host'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x105): undefined reference to `enet_initialize'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x204): undefined reference to `enet_packet_create'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x217): undefined reference to `enet_peer_send'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x29f): undefined reference to `enet_packet_create'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x2b3): undefined reference to `enet_peer_send'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x5af): undefined reference to `enet_host_destroy'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x65b): undefined reference to `enet_host_create'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x91f): undefined reference to `enet_host_create'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x950): undefined reference to `enet_host_connect'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x96c): undefined reference to `enet_host_service'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x1023): undefined reference to `enet_host_service'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x1437): undefined reference to `enet_packet_destroy'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x121): undefined reference to `enet_deinitialize'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/rvwp.dir/build.make:991: bin/rvwp.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/rvwp.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
Cross-compiling Enet
To cross compile enet, I use the following script
#!/bin/bash
TOOLSET="x86_64-w64-mingw32"
wget http://enet.bespin.org/download/enet-1.3.13.tar.gz
tar -xzf enet-1.3.13.tar.gz
cd enet-1.3.13
./configure \
--build=${TOOLSET} \
--host=x86_64-windows \
--target=${TOOLSET} \
--prefix=/usr/local/mingw64 \
--enable-shared
make -j3
sudo make install
My Program
I'm using cmake to generate the makefiles, and a toolchain to allow cross-compilation. The program compiles with SFML, thor, std::thread, and Lua fine. None of these libraries use GNU autoconf
My CMakeLists.txt looks like this:
find_package(ENet REQUIRED)
include_directories(${ENET_INCLUDE_DIR})
set(BASE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT} ${LUA_LIBRARY} ${CMAKE_DL_LIBS} ${ENET_LIBRARY})
set(BASE_LIBRARIES ${BASE_LIBRARIES} wsock32 ws2_32 winmm)
message(${BASE_LIBRARIES})
set(EXECUTABLE_NAME "rvwp")
add_executable(${EXECUTABLE_NAME} WIN32 ${CLIENT_SRC})
set_target_properties(${EXECUTABLE_NAME} PROPERTIES BUILD_WITH_INSTALL_RPATH true)
install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)
target_link_libraries(${EXECUTABLE_NAME} ${BASE_LIBRARIES} ${SFML_LIBRARIES} ${SFGUI_LIBRARY} ${THOR_LIBRARY})
The findENet file looks like this:
FIND_PATH(ENET_INCLUDE_DIR enet/enet.h
PATHS
$ENV{ENETDIR}
/usr/local
/usr
PATH_SUFFIXES include)
FIND_LIBRARY(ENET_LIBRARY
NAMES enet
PATHS
$ENV{ENETDIR}
/usr/local
/usr
PATH_SUFFIXES lib)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(ENet DEFAULT_MSG ENET_LIBRARY ENET_INCLUDE_DIR)
IF (ENet_FOUND)
MESSAGE(STATUS "Found ENet in ${ENET_INCLUDE_DIR}")
IF(WIN32)
SET(WINDOWS_ENET_DEPENDENCIES "ws2_32;winmm")
SET(ENET_LIBRARIES ${ENET_LIBRARY} ${WINDOWS_ENET_DEPENDENCIES})
ELSE(WIN32)
SET(ENET_LIBRARIES ${ENET_LIBRARY})
ENDIF(WIN32)
ENDIF (ENet_FOUND)
MARK_AS_ADVANCED(ENET_LIBRARY ENET_LIBRARIES ENET_INCLUDE_DIR)
The tool chain looks like this:
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64 g++-mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-Ubuntu-mingw64.cmake ..
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++" )
# target environment on the build host system
# set 1st to dir with the cross compiler's C/C++ headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} /usr/local/mingw64 ./extlibs)
set(LUA_INCLUDE_DIR /usr/local/mingw64/include/lua5.1/)
set(LUA_LIBRARY /usr/local/mingw64/lib/liblua5.1.a)
set(OPENAL_LIBRARY /usr/local/mingw64/lib/libopenal32.a)
# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
The linklibs.rsp file is used to pass linking commands to the linker, here is its value:
/usr/local/mingw64/lib/liblua5.1.a /usr/local/mingw64/lib/libenet.a -lwsock32 -lws2_32 -lwinmm /usr/local/mingw64/lib/libsfml-system.a /usr/local/mingw64/lib/libsfml-window.a /usr/local/mingw64/lib/libsfml-graphics.a /usr/local/mingw64/lib/libsfml-network.a /usr/local/mingw64/lib/libsfml-audio.a /usr/local/mingw64/bin/sfgui.dll /usr/local/mingw64/bin/libthor.dll -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
(EDIT) AR Formats
Extracting the not-working libenet.a and using file results in this:
callbacks.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), with debug_info, not stripped
Extracting the working libsfml-graphics.a results in this:
d000508.o: Intel amd64 COFF object file, no line number info, not stripped, 5 sections, symbol offset=0x144, 8 symbols
So it appears that the problem is in compiling enet

Make unable to link something it literally just compiled

I am in a rather small hobby project, we are creating a game, or at least trying to do so... The main issue we are facing is the fact that we are able to compile the project on MacOS and Linux, but not on Windows.
We are using GLFW as an interface to OpenGL, so it is required for us to get anything visible on the screen. Problem is that when we tell Make to do its thing, it compiles each individual module but fails to link them because of undefined references. While that may make sense the weird thing is that these unknown references all have the prefix __imp_, which isn't present in source. Example: We used the method glfwInit but when trying to make the project we get: graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0xa): undefined reference to `_imp__glfwInit'
Having done some research I found This SO Question of which i tried the answer. It failed, due to the fact that the only lib in there after compilation is libglfw3.a, no sort of dll or hints at a dll. Renaming this file accordingly did not help, neither did placing the precompiled binary. Deleting this file results in it recompiling and reappearing, so clearly make knows where it is.
We also tried switching to clang because maybe it has a more intelligent linker, but it didn't want to compile anything in the first place.
I performed a lot of research on this and I found countless SO answers and forum threads of people with similar or even identical problems, every single one had a different solution and none of them worked for us.
To generate the Makefile we are using CMake with the following script in the main subdirectory (don't worry, it gets called from above, we got that working so far):
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(graphicsengine)
add_subdirectory(fileSys)
add_subdirectory(networking)
add_subdirectory(game)
add_subdirectory(glfw-3.2.1)
include_directories(glfw-3.2.1/include)
if (UNIX AND NOT APPLE)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
endif()
add_executable(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
target_link_libraries(${PROJECT_NAME} graphicsengine fileSys networking game)
if (APPLE)
target_link_libraries(${PROJECT_NAME} "-framework OpenGL")
endif()
if (WIN32)
target_link_libraries(${PROJECT_NAME} opengl32 gdi32)
endif()
if (UNIX AND NOT APPLE)
target_link_libraries(${PROJECT_NAME} ${OPENGL_gl_LIBRARY})
endif()
target_link_libraries(${PROJECT_NAME} glfw)
install(TARGETS ${PROJECT_NAME} DESTINATION ${INSTALL_DIR})
The rest of the important cmake calls are in the parent directory
Here is a full log of cmake/make that a custom build script calls:
debug
release
-- The CXX compiler identification is GNU 5.3.0
-- Check for working CXX compiler: D:/MinGW/bin/g++.exe
-- Check for working CXX compiler: D:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The C compiler identification is GNU 5.3.0
-- Check for working C compiler: D:/MinGW/bin/gcc.exe
-- Check for working C compiler: D:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C 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
-- Could NOT find Vulkan (missing: VULKAN_INCLUDE_DIR)
-- Looking for dinput.h
-- Looking for dinput.h - not found
-- Looking for xinput.h
-- Looking for xinput.h - not found
-- Performing Test _GLFW_HAS_DEP
-- Performing Test _GLFW_HAS_DEP - Success
-- Performing Test _GLFW_HAS_ASLR
-- Performing Test _GLFW_HAS_ASLR - Success
-- Performing Test _GLFW_HAS_64ASLR
-- Performing Test _GLFW_HAS_64ASLR - Failed
-- Using Win32 for window creation
-- Configuring done
-- Generating done
-- Build files have been written to: D:/cpp-neon/Workspace/Supermerged
--------------MAKE STARTS HERE--------------
[ 60%] Built target glfw
[ 68%] Built target graphicsengine
[ 76%] Built target fileSys
[ 84%] Built target networking
[ 92%] Built target game
[ 96%] Linking CXX executable Submerged_PR0.exe
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0xa): undefined reference to `_imp__glfwInit'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x45): undefined reference to `_imp__glfwCreateWindow'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x55): undefined reference to `_imp__glfwTerminate'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x64): undefined reference to `_imp__glfwMakeContextCurrent'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x71): undefined reference to `_imp__glfwWindowShouldClose'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x98): undefined reference to `_imp__glfwSwapBuffers'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x9f): undefined reference to `_imp__glfwPollEvents'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0xa8): undefined reference to `_imp__glfwTerminate'
collect2.exe: error: ld returned 1 exit status
source\CMakeFiles\Submerged_PR0.dir\build.make:101: recipe for target 'source/Submerged_PR0.exe' failed
mingw32-make[2]: *** [source/Submerged_PR0.exe] Error 1
CMakeFiles\Makefile2:102: recipe for target 'source/CMakeFiles/Submerged_PR0.dir/all' failed
mingw32-make[1]: *** [source/CMakeFiles/Submerged_PR0.dir/all] Error 2
Makefile:128: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Make Failed, Aborting Install
PS D:\cpp-neon\Workspace\Supermerged>
In case you want to know, this is our project structure:
The CMakeLists.txt above is the one you see in /source
The rest of the modules are built in the same way, except for glfw, which is the one you can find on their website
At this point in time we are more or less helpless and tired of looking for a solution, at the point where we just try stuff until it kind of works but ends up a giant mess of everything that should never have existed, we all know how that ends up... (cough) which of these branches was the one where i did the thing that made it do the other thing?(cough) We hope that some of you (SO) might have a better approach to finding the solution than any of us. Feel free to ask anything else that might help you find something that works!
Thanks in advance!
In the name of: sDev
We found it!
The problem was actually in our graphics.hpp:
We had the line #define GLFW_DLL
Which was wrong because it had to be
#define GLFW_STATIC
And now it works