I am trying to set up SDL in CLion with MinGW, but I get errors after errors, and I just can't imagine where do I go wrong. Now I have this Cmake code, and I get the error undefined reference to 'WinMain#16' . I suppose I forget to link something, probably SDLmain, but I think I wrote it there in the target_link_libraries(). I would greatly appreciate some help to make it work.
cmake_minimum_required(VERSION 3.15)
project(SDL_project)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(SDL_project main.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY} -lmingw32 -lSDL2main -lSDL2 -mwindows)
The error message:
====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
Scanning dependencies of target SDL_project
[ 50%] Building CXX object CMakeFiles/SDL_project.dir/main.cpp.obj
[100%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o): in function `main':
D:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
Makefile:117: recipe for target 'SDL_project' failed
And I also made a cmake folder in the project folder, for the SDL find, but I am not sure if it is necessary.
set(FIND_SDL2_PATHS D:/SDL2/SDL2-devel-2.0.12-mingw/SDL2-2.0.12/x86_64-w64-mingw32)
find_path(SDL2_INCLUDE_DIR SDL2
PATH_SUFFIXES include
PATHS ${FIND_SDL2_PATHS})
find_library(SDL2_LIBRARY
NAMES SDL2
PATH_SUFFIXES lib
PATHS ${FIND_SDL2_PATHS})
The solution was to add a new line to the end: add_definitions(-DSDL_MAIN_HANDLED).
Now the CMakeLists.txt code looks like this:
cmake_minimum_required(VERSION 3.15)
project(SDL_project)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(SDL_project main.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY} -lmingw32 -mwindows)
add_definitions(-DSDL_MAIN_HANDLED)
And in the project folder in a separete folder called cmake there is the FindSDL2.cmake file, to locate SDL2:
set(FIND_SDL2_PATHS D:/SDL2/SDL2-devel-2.0.12-mingw/SDL2-2.0.12/x86_64-w64-mingw32)
find_path(SDL2_INCLUDE_DIR SDL2
PATH_SUFFIXES include
PATHS ${FIND_SDL2_PATHS})
find_library(SDL2_LIBRARY
NAMES SDL2 SDL2main
PATH_SUFFIXES lib
PATHS ${FIND_SDL2_PATHS})
From the CMakeLists.txt I removed -lSDL2 and -lSDL2main flags, because they resulted in errors:
====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
[ 50%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2
A:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
Makefile:117: recipe for target 'SDL_project' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
Also, it is important to use 64-bit mingw-w64, because mingw-w32 also gives errors, like:
====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
[ 50%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\SDL_project.dir/objects.a(main.cpp.obj):D:/SDL_project/main.cpp:17: undefined reference to `SDL_Init'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\SDL_project.dir/objects.a(main.cpp.obj): in function `main':
D:/SDL_project/main.cpp:19: undefined reference to `SDL_GetError'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:24: undefined reference to `SDL_CreateWindow'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:27: undefined reference to `SDL_GetError'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:32: undefined reference to `SDL_GetWindowSurface'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:35: undefined reference to `SDL_MapRGB'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:35: undefined reference to `SDL_FillRect'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:38: undefined reference to `SDL_UpdateWindowSurface'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:41: undefined reference to `SDL_Delay'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:45: undefined reference to `SDL_DestroyWindow'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:48: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
Makefile:117: recipe for target 'SDL_project' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
And, the SDL.dll file from the binary folder has to be added to the compiler, to avoid exit code -1073741515 (0xC0000135). It can be done by setting a path to the folder, or by copying the SDL.dll from the SDL\bin folder to the mingw64\bin folder.
Related
I'm trying to include TensorFlow lite libraries in CMakeLists.txt of C++ project. I followed the instructure in https://www.tensorflow.org/lite/guide/build_cmake
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
mkdir tflite_build && cd tflite_build
cmake ../tensorflow_src/tensorflow/lite -DTFLITE_ENABLE_XNNPACK=OFF
-DTFLITE_ENABLE_EXTERNAL_DELEGATE=OFF TFLITE_ENABLE_RUY=ON/OFF (tried both option)
cmake --build . --config release -j24
and added all the libs to the CmakeLists.txt file as follow
cmake_minimum_required(VERSION 3.0.0)
project(main)
set(TENSORFLOW_SRC_DIR "test_cpp/tensorflow_src")
set(TFLITE_DIR "test_cpp/tflite_build")
find_package(OpenCV 4 REQUIRED)
file(GLOB SOURCE_FILES src/*.cpp src/*.h )
message(${SOURCE_FILES})
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
include_directories(
${TENSORFLOW_SRC_DIR}
${TFLITE_DIR}/eigen
${TFLITE_DIR}/neon2sse
${TFLITE_DIR}/abseil-cpp
${TFLITE_DIR}/farmhash/src
${TFLITE_DIR}/flatbuffers/include
${TFLITE_DIR}/gemmlowp/public
${TFLITE_DIR}/gemmlowp
${TFLITE_DIR}/ruy
${TFLITE_DIR}/cpuinfo/include
)
target_link_libraries(
${CMAKE_PROJECT_NAME} PUBLIC
${OpenCV_LIBS}
${TFLITE_DIR}/_deps/farmhash-build/libfarmhash.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_marshalling.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_program_name.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_reflection.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_private_handle_accessor.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_config.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_commandlineflag_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_commandlineflag.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cordz_functions.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_strings_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cordz_handle.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_str_format_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cordz_info.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cord.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cord_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_strings.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/container/libabsl_raw_hash_set.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/container/libabsl_hashtablez_sampler.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/synchronization/libabsl_synchronization.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/synchronization/libabsl_graphcycles_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/hash/libabsl_city.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/hash/libabsl_hash.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/hash/libabsl_low_level_hash.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/time/libabsl_civil_time.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/time/libabsl_time_zone.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/time/libabsl_time.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/profiling/libabsl_exponential_biased.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/types/libabsl_bad_optional_access.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/types/libabsl_bad_variant_access.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/numeric/libabsl_int128.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_raw_logging_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_throw_delegate.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_base.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_log_severity.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_spinlock_wait.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_malloc_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/status/libabsl_status.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/debugging/libabsl_stacktrace.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/debugging/libabsl_demangle_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/debugging/libabsl_symbolize.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/debugging/libabsl_debugging_internal.a
${TFLITE_DIR}/_deps/flatbuffers-build/libflatbuffers.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_ctx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_kernel_avx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_have_built_path_for_avx512.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_cpuinfo.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_frontend.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_blocking_counter.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_kernel_avx512.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_apply_multiplier.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_block_map.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_trmul.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_kernel_avx2_fma.a
${TFLITE_DIR}/_deps/ruy-build/ruy/profiler/libruy_profiler_instrumentation.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_denormal.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_prepacked_cache.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_system_aligned_alloc.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_context_get_ctx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_allocator.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_have_built_path_for_avx2_fma.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_pack_avx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_wait.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_context.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_pack_avx2_fma.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_thread_pool.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_pack_arm.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_tune.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_kernel_arm.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_have_built_path_for_avx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_prepare_packed_matrices.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_pack_avx512.a
${TFLITE_DIR}/_deps/fft2d-build/libfft2d_fftsg.a
${TFLITE_DIR}/_deps/fft2d-build/libfft2d_fftsg2d.a
${TFLITE_DIR}/_deps/clog-build/libclog.a
${TFLITE_DIR}/_deps/cpuinfo-build/libcpuinfo.a
${TFLITE_DIR}/libtensorflow-lite.a
dl
)
It worked on a windows machine but on Linux I'm getting error about
undefined reference to `ruy::ScopedSuppressDenormals::ScopedSuppressDenormals()'
[ 33%] Building CXX object CMakeFiles/main.dir/src/main.cpp.o
[ 66%] Building CXX object CMakeFiles/main.dir/src/ssd_mobilenet_tflite.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: ../tflite_build/libtensorflow-lite.a(interpreter.cc.o): in function `tflite::Interpreter::Invoke()':
interpreter.cc:(.text+0x3ff): undefined reference to `ruy::ScopedSuppressDenormals::ScopedSuppressDenormals()'
/usr/bin/ld: interpreter.cc:(.text+0x523): undefined reference to `ruy::ScopedSuppressDenormals::~ScopedSuppressDenormals()'
/usr/bin/ld: ../tflite_build/libtensorflow-lite.a(interpreter.cc.o): in function `tflite::Interpreter::Invoke() [clone .cold]':
interpreter.cc:(.text.unlikely+0x5c): undefined reference to `ruy::ScopedSuppressDenormals::~ScopedSuppressDenormals()'
/usr/bin/ld: CMakeFiles/main.dir/src/ssd_mobilenet_tflite.cpp.o: in function `SSD_MOBILENET::loadModel(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
ssd_mobilenet_tflite.cpp:(.text+0x2b2): undefined reference to `tflite::ops::builtin::BuiltinOpResolver::BuiltinOpResolver()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:231: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I don't want to add the libs as "add_subdirectory" as it was given in the guide because it will compile every time I create a new project
So how can I solve the undefined reference to `ruy::ScopedSuppressDenormals::ScopedSuppressDenormals()'
UPDATE:
if I change the CMakeLists.txt to :
cmake_minimum_required(VERSION 3.0.0)
project(main)
set(TENSORFLOW_SRC_DIR "test_cpp/tensorflow_src")
set(TFLITE_DIR "test_cpp/tflite_build")
find_package(OpenCV 4 REQUIRED)
find_package(TFLITE)
file(GLOB SOURCE_FILES src/*.cpp src/*.h )
message(${SOURCE_FILES})
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
include_directories(
${TENSORFLOW_SRC_DIR}
)
target_link_libraries(
${CMAKE_PROJECT_NAME} PUBLIC
${OpenCV_LIBS}
${TFLITE_LIBS}
)
I take this error
from test_cpp/src/main.cpp:1:
test_cpp/tensorflow_src/tensorflow/lite/interpreter_builder.h:26:10: fatal error: flatbuffers/flatbuffers.h: No such file or directory
26 | #include "flatbuffers/flatbuffers.h" // from #flatbuffers
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/main.dir/build.make:63: CMakeFiles/main.dir/src/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I found this repo that contains all needed libs for the project https://github.com/muhammedakyuzlu/tflite-cpp-package
I believe that the problem you have is the ordering of the libs.
Try putting the libtensorflow-lite.a before the ruy-*.a in your setup.
There is another trick that could help you getting started.
Just add this to your CMake File:
include(FetchContent)
FetchContent_Declare(
tensorflow
GIT_REPOSITORY https://github.com/tensorflow/tensorflow.git
GIT_PROGRESS TRUE
GIT_SHALLOW TRUE
GIT_TAG v2.10.0
SOURCE_SUBDIR tensorflow/lite
)
FetchContent_MakeAvailable(tensorflow)
and link to the library:
target_link_libraries(${PROJECT_NAME} PRIVATE
tensorflow-lite
)
This is downloading all dependencies automatically and should make you ready to go.
I'm interested in statically linking PROJ to a library created with pybind11.
but, I get error by cmake.
Why am I getting this error and how can I fix it?
My CMakeLists.txt is this.
cmake_minimum_required(VERSION 3.4)
project(myearth LANGUAGES CXX)
add_subdirectory(pybind11)
pybind11_add_module(myearth MyEarth.cpp)
include_directories(${CMAKE_SOURCE_DIR}/PROJ/include)
add_library(proj4 STATIC IMPORTED)
set_target_properties(proj4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/PROJ/lib/libproj.a)
target_link_libraries(myearth PRIVATE proj4)
CMake Error.
I think fPIC option is for shared library. so, libproj.a is not necessary this option because this is static library.
(venv) ~/Projects/LinkLibTest/MySample/build$ make
[ 50%] Building CXX object CMakeFiles/myearth.dir/MyEarth.cpp.o
[100%] Linking CXX shared module myearth.cpython-36m-x86_64-linux-gnu.so
/usr/bin/ld: ../PROJ/lib/libproj.a(proj_4D_api.o): relocation R_X86_64_PC32 against symbol `pj_errno' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/myearth.dir/build.make:98: myearth.cpython-36m-x86_64-linux-gnu.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/myearth.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
(venv) ~/Projects/LinkLibTest/MySample/build$
I created libproj.a by the following command.
$ ./configure --prefix=/output --disable-shared
$ sudo make
$ sudo make install
Linux Mint 20.3 64bit
CMake v3.22.2
PROJ v5.2.0 (https://proj.org/)
Python v3.6.8
Thank you.
Shared libraries must have position independent code, but your version of PROJ was built without it; you will have to rebuild PROJ with PIC.
$ ./configure --prefix=/output --with-pic
Below is the CmakeLists file that I'm using. I'm using the linker flags from eclipse as it seems to build in eclipse properly.
project(new1 LANGUAGES CXX C)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 14)
# Compiler options for compiling MinGW targets.
set(CMAKE_C_FLAGS_DEBUG "-D_WIN32_WINNT=0x0601 -DprojCOVERAGE_TEST=0 -fmessage-length=0 -Wcast-qual")
#-------------------------------------------------------------
# Linker options for Linking MinGW targets.
set(CMAKE_EXE_LINKER_FLAGS "-Xlinker -lwinmm")
#-------------------------------------------------------------
include_directories(FreeRTOS/Source/include
FreeRTOS/Source/portable/MSVC-MingW
/)
add_subdirectory(FreeRTOS/Source)
add_executable(application main.cpp)
target_link_libraries(application kernel)
target_link_libraries(application winmm.lib)
The below is the build output
"C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe" -E cmake_link_script CMakeFiles\application.dir\link.txt --verbose=1
"C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe" -E remove -f CMakeFiles\application.dir/objects.a
"F:\TrueSTUDIO for STM32 9.0.0\PCTools\bin\ar.exe" cr CMakeFiles\application.dir/objects.a #CMakeFiles\application.dir\objects1.rsp
"F:\TrueSTUDIO for STM32 9.0.0\PCTools\bin\g++.exe" -g -Xlinker -lwinmm -Wl,--whole-archive CMakeFiles\application.dir/objects.a -Wl,--no-whole-archive -o application.exe -Wl,--out-implib,libapplication.dll.a -Wl,--major-image-version,0,--minor-image-version,0 #CMakeFiles\application.dir\linklibs.rsp
FreeRTOS/Source/portable/MSVC-MingW/libport.a(port.c.obj):port.c:(.text+0x15): undefined reference to `timeGetDevCaps#8'
FreeRTOS/Source/portable/MSVC-MingW/libport.a(port.c.obj):port.c:(.text+0x2d): undefined reference to `timeBeginPeriod#4'
FreeRTOS/Source/portable/MSVC-MingW/libport.a(port.c.obj):port.c:(.text+0xd5): undefined reference to `timeGetDevCaps#8'
FreeRTOS/Source/portable/MSVC-MingW/libport.a(port.c.obj):port.c:(.text+0xe7): undefined reference to `timeEndPeriod#4'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [application.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/application.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/application.dir/rule] Error 2
mingw32-make.exe: *** [application] Error 2
Can somebody help please.
So, there was no issue with linking the winmm library it is just that I'm linking this to a wrong target. It was a dependency of the windows port in freeRTOS/Source/Portable.
I have started with a project that are using the simple2d library (https://github.com/simple2d/simple2d). But when I try to build my small example project I get a DSO missing error. I think a have manage to link the dependencies for simple2d, but apparently I'm still some thing.
Here is the output:
> ====================[ Build | c_game | Debug ]==================================
/home/fredrik/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/192.5728.100/bin/cmake/linux/bin/cmake --build /home/fredrik/Repos/c_game/cmake-build-debug --target c_game -- -j 2 -m
[ 50%] Linking C executable c_game
/usr/bin/ld: /usr/local/lib/libsimple2d.a(gl.o): undefined reference to symbol 'fmin##GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/c_game.dir/build.make:85: recipe for target 'c_game' failed
make[3]: *** [c_game] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/c_game.dir/all' failed
make[2]: *** [CMakeFiles/c_game.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/c_game.dir/rule' failed
make[1]: *** [CMakeFiles/c_game.dir/rule] Error 2
Makefile:118: recipe for target 'c_game' failed
make: *** [c_game] Error 2
And here is my CMakeList.txt, I hope someone can help me.
cmake_minimum_required(VERSION 3.14)
project(c_game C)
set(CMAKE_C_STANDARD 11)
find_package(SDL2 REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(c_game main.c)
find_library(SIMPLE2D_LIB libsimple2d.a /usr/local/lib)
message(${SIMPLE2D_LIB})
message(${SDL2_INCLUDE_DIRS})
message(${GLEW_INCLUDE_DIRS})
target_include_directories(c_game PUBLIC ${SDL2_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS})
target_link_libraries(c_game ${SIMPLE2D_LIB} ${SDL2_LIBRARIES} ${GLEW_LIBRARIES})
Disclaimer: I'm new to cmake so i have no idea what I'm doing.
All guides and tutorials i find seem to think I'm running a 20 man team that needs to work together.
All I'm trying to do is put all my libraries in an include folder and lib folder.
In visual studio and code::blocks i just set up the linker in the IDE, but cmake is a little hard for me to wrap my head around.
I've set include and link directories with
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
Then i try to
target_link_libraries(${PROJECT_NAME} jsoncpp)
This however produces undefined references. Is there a simple way to add and link libraries without me having to google and add a cmake module for each one?
Edit:
attempts:
target_link_libraries(${PROJECT_NAME} libjsoncpp.a)
error: undefined reference
target_link_libraries(${PROJECT_NAME} libjsoncpp)
error: cannot find -llibjsoncpp
target_link_libraries(${PROJECT_NAME} jsoncpp)
error: undefined reference
target_link_libraries(${PROJECT_NAME} C:/Repos/prosjekt-bad-racoon/lib/libjsoncpp.a)
error: undefined reference
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/lib/libjsoncpp.a)
error: undefined reference
Edit:
Full cmake file
cmake_minimum_required(VERSION 3.8)
project(prosjekt_bad_racoon)
set(CMAKE_CXX_STANDARD 11)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(SOURCE_FILES main.cpp Core.cpp Core.h Collider.cpp Collider.h Player.cpp Player.h Texture.cpp Texture.h AudioController.cpp AudioController.h)
add_executable(prosjekt_bad_racoon ${SOURCE_FILES})
set(EXECUTABLE_NAME ${PROJECT_NAME})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
target_link_libraries(${PROJECT_NAME} libjsoncpp.a)
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
link_directories("${PROJECT_SOURCE_DIR}")
endif()
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build C:\Repos\prosjekt-bad-racoon\cmake-build-debug --target prosjekt_bad_racoon -- -j 2
[ 14%] Linking CXX executable prosjekt_bad_racoon.exe
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreC2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:3: undefined reference to `Json::Value::Value(char const*)'
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreD2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:8: undefined reference to `Json::Value::~Value()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\prosjekt_bad_racoon.dir\build.make:236: recipe for target 'prosjekt_bad_racoon.exe' failed
mingw32-make.exe[3]: *** [prosjekt_bad_racoon.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/prosjekt_bad_racoon.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/prosjekt_bad_racoon.dir/rule] Error 2
Makefile:117: recipe for target 'prosjekt_bad_racoon' failed
mingw32-make.exe: *** [prosjekt_bad_racoon] Error 2