How to link library in CLion - linux

I'm trying to use NTL library for ZZ class, and would like to use dedicated functions. Unfortunately during compilation I'm getting a lot of errors:
[100%] Linking CXX executable hpc5
CMakeFiles/hpc5.dir/main.cpp.o: In function `findX(NTL::ZZ, NTL::ZZ, NTL::ZZ)':
/home/rooter/CLionProjects/hpc5/main.cpp:44: undefined reference to `find_xi(NTL::ZZ, NTL::ZZ)'
/home/rooter/CLionProjects/hpc5/main.cpp:57: undefined reference to `chinese_remainder(NTL::ZZ*, NTL::ZZ*, NTL::ZZ)'
/home/rooter/CLionProjects/hpc5/main.cpp:58: undefined reference to `NTL::operator<<(std::ostream&, NTL::ZZ const&)'
CMakeFiles/hpc5.dir/main.cpp.o: In function `NTL::ZZ::ZZ(NTL::ZZ const&)':
/usr/include/NTL/ZZ.h:58: undefined reference to `_ntl_gcopy(void*, void**)'
CMakeFiles/hpc5.dir/main.cpp.o: In function `NTL::ZZ::operator=(NTL::ZZ const&)':
/usr/include/NTL/ZZ.h:73: undefined reference to `_ntl_gcopy(void*, void**)'
CMakeFiles/hpc5.dir/main.cpp.o: In function `NTL::ZZ::operator=(long)':
/usr/include/NTL/ZZ.h:75: undefined reference to `_ntl_gintoz(long, void**)'
I have installed libntl-dev on my linux mint, added set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -lntl" ) to my CMakeLists.txt and set CMake option -lntl and it has no effect. How can I link this library?
My CMakeLists.txt contains:
cmake_minimum_required(VERSION 3.10)
project(hpc5)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -lntl" )
add_executable(hpc5 main.cpp)

If you want to link to a runtime library using CMake, you need to use target_link_libraries command. For example, you may change your CMakeLists.txt file as follows:
cmake_minimum_required(VERSION 3.10)
project(hpc5)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
add_executable(hpc5 main.cpp)
target_link_libraries(hpc5 ntl)
This is assuming CMake is able to find the NTL library in your system.
EDIT : Fix executable name typo.

Related

Clion / Cmake can't find boost on linux

I recently changed to Linux Mint Debian Edition and cannot include boost to Clion using cmake. Yesterday I tried every option I could find, including on stackoverflow, but nothing has worked yet.
Boost has been installed using: sudo apt -y install libboost-filesystem-dev
This is a version I found in this thread: How to include external library (boost) into CLion C++ project with CMake?
which supposedly worked, but it didn't.
cmake_minimum_required(VERSION 3.22)
project(boost_project)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost COMPONENTS system filesystem REQUIRED)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(boost_project main.cpp)
if(Boost_FOUND)
target_link_libraries(boost_project ${Boost_LIBRARIES})
endif()
The Cmake output message is:
CMake Error at /app/extra/clion/bin/cmake/linux/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Boost (missing: Boost_INCLUDE_DIR system filesystem)
I've gone through dozens of threads but so far nothing has worked. Has anyone suggestions of what's wrong?
EDIT:
So I've de-installed Clion and re-installed it with snap, since I read in another thread that it was an issue with installing Clion through the software center. Now I can include boost with no problem, but building it reveals the compiler still doesn't like using it.
#include <iostream>
#include "boost/asio.hpp"
boost::asio::io_service io_s;
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
: && /usr/bin/c++ -g CMakeFiles/BoostTest.dir/main.cpp.o -o BoostTest && :
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_event::posix_event()': /usr/include/boost/asio/detail/impl/posix_event.ipp:42: undefined reference to pthread_condattr_setclock'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::~posix_thread()': /usr/include/boost/asio/detail/impl/posix_thread.ipp:35: undefined reference to pthread_detach'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::join()': /usr/include/boost/asio/detail/impl/posix_thread.ipp:42: undefined reference to pthread_join'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)': /usr/include/boost/asio/detail/impl/posix_thread.ipp:59: undefined reference to pthread_create'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_signal_blocker::posix_signal_blocker()': /usr/include/boost/asio/detail/posix_signal_blocker.hpp:43: undefined reference to pthread_sigmask'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_signal_blocker::~posix_signal_blocker()': /usr/include/boost/asio/detail/posix_signal_blocker.hpp:50: undefined reference to pthread_sigmask'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
The solution to the first problem, cmake not finding boost, had nothing to do with cmake but Clion. Installing Clion with flatpak can apparently cause this error. To fix it I just needed to de-install Clion and re-install it with snap.
The second issue was cmake not finding several thread related includes. For that cmake needed
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
to be added. I found this solution in these threads:
linking errors for boost for visual studio linux project
Undefined reference to pthread_create in Linux
The complete and working cmake looks like this now:
cmake_minimum_required(VERSION 3.22)
project(boost_project)
message(STATUS "start running cmake...")
find_package(Boost COMPONENTS system)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
set(CMAKE_CXX_STANDARD 14)
add_executable(boost_project main.cpp)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()
target_link_libraries(boost_project ${Boost_LIBRARIES})

gcc dialog library not linking

I'm trying to develop a small utility using the dialog library in C (the dialog command in linux).
On fedora linux works fine, but if i try to compile it on debian with the command:
gcc -ldialog -lncurses -I/usr/include dialog_test.c
I get the following error:
vetinari#ankhmorpork:~/Projects/Other/test$ gcc -ldialog -I/usr/include dialog_test.c
/usr/bin/ld: /tmp/ccX6fPYB.o: warning: relocation against `dialog_vars' in read-only section `.text'
/usr/bin/ld: /tmp/ccX6fPYB.o: in function `main':
dialog_test.c:(.text+0x5c): undefined reference to `init_dialog'
/usr/bin/ld: dialog_test.c:(.text+0x79): undefined reference to `dialog_yesno'
/usr/bin/ld: dialog_test.c:(.text+0xae): undefined reference to `dialog_menu'
/usr/bin/ld: dialog_test.c:(.text+0xbc): undefined reference to `dialog_vars'
/usr/bin/ld: dialog_test.c:(.text+0xc5): undefined reference to `end_dialog'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
The dialog command works fine.
Anyone has any idea why it isn't working on debian?
(Answer for the wiki sake, in case someone comes by here later)
You have to put the libraries you want to link at the end of the gcc command, like this:
gcc dialog_test.c -ldialog -lncurses
The reason is explained here: The way the linker looks up symbols it has to first see the reference, and then the library prodiving the symbol
Additionally, the dialog library might have other dependencies than ncurses. There is explanation how to find out what to include and what to link here, in short: dialog-config should tell you about it.
In this specific case, what worked for me (ubuntu 20.04) was linking ncursesw instead of ncurses.
After that, I was left with an
undefined reference to `sqrt'
linker error, which can be solved by linking the match library using -lm.
So, in total, this command works:
gcc dialog_test.c -ldialog -lncursesw -lm

OpenCV 3.2.0 linker error while building a project using cmake

I mistakenly removed my /usr/local folder after which I had to build OpenCV and Caffe again. Now my projects are not compiling. Each time I try to build the project using cmake there is an error the stops the progress.
[ 80%] Built target mygoturn
[ 80%] Linking CXX executable ../bin/runTracker
/usr/bin/ld: CMakeFiles/runTracker.dir/src/visualizer/runTracker.cpp.o: undefined reference to symbol '_ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi'
//usr/local/lib/libopencv_imgcodecs.so.4.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/runTracker.dir/build.make:200: recipe for target '../bin/runTracker' failed
make[2]: *** [../bin/runTracker] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/runTracker.dir/all' failed
make[1]: *** [CMakeFiles/runTracker.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I checked with the command pkg-config --libs --cflags opencv
and got the following response given below:
-I/usr/include/opencv -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_datasets -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_video -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_viz -lopencv_surface_matching -lopencv_text -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
I am including then CMakeLists.txt file that contains the build sequence.
cmake_minimum_required(VERSION 3.6)
project(mygoturn)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(Boost COMPONENTS system filesystem regex REQUIRED)
# avoid "can not find -lopencv_dep_cudart"
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
find_package( OpenCV REQUIRED )
message("Open CV version is ${OpenCV_VERSION}")
set(Caffe_DIR ~/tracking/GOTURN/caffe)
find_package(Caffe REQUIRED)
include_directories(${Caffe_INCLUDE_DIRS})
add_definitions(${Caffe_DEFINITIONS}) # ex. -DCPU_ONLY
message("Caffe_DIR is ${Caffe_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
add_library (${PROJECT_NAME}
src/network/regressor.cpp
src/network/regressor_base.cpp
src/tracker/tracker.cpp
src/helper/helper.cpp
src/helper/bounding_box.cpp
src/helper/image_proc.cpp
src/network/regressor.h
src/network/regressor_base.h
src/tracker/tracker.h
src/helper/helper.h
src/helper/bounding_box.h
src/helper/image_proc.h
)
include_directories(src)
set(SOURCE_FILES src/visualizer/runTracker.cpp src/loader/loader_base.cpp src/loader/loader_base.h)
add_executable(runTracker ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${Caffe_LIBRARIES} ${Boost_LIBRARIES})
target_link_libraries (runTracker ${PROJECT_NAME})
After buidling the new version of the OpenCV library (opencv4), I found that there has been few implementation changes made to the highgui module, wherein few of the implementations present on the highgui module has been shifted to imgcodecs (mainly the imread and imwrite).
Setting the CMAKE_CXX_FLAGS more specifically to include all the libraries or specifically the -lopencv_imgcodecs would suffice.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lopencv_imgcodecs")
This happened to resolve the issue that I had been facing. The pkg-config --libs opencv lists all of the library modules that have been built in opencv4.

gcc -lm doesn't fix undefined reference to `atan'

Eclipse 3.8.1 Linux Mint
Added -lm to Project / C/C++ Build / Settings / GCC C Linker / Command:
gcc -lm
Console messages:
Building target: Nicomedes
Invoking: GCC C Linker
gcc -lm -o "Nicomedes" ./Nicomedes.o
./Nicomedes.o: In function `main':
/home/bogwan/work/Nicomedes/Debug/../Nicomedes.c:244: undefined reference to `pow'
/home/bogwan/work/Nicomedes/Debug/../Nicomedes.c:258: undefined reference to `atan'
/home/bogwan/work/Nicomedes/Debug/../Nicomedes.c:260: undefined reference to `atan'
collect2: error: ld returned 1 exit status
make: *** [Nicomedes] Error 1
07:29:58 Build Finished (took 108ms)
Your linkage order is wrong. Make it gcc -o "Nicomedes" ./Nicomedes.o -lm.
In the linkage sequence, files that need symbol definitions must come before
the ones that provide the definitions. So libraries after object files.
In Eclipse, the setting C/C++ Build -> Settings -> GCC C Linker -> Command
is intended to set the program that you want to do your linking, not your linkage
options. Set library options in C/C++ Build -> Settings -> GCC C Linker -> Libraries

undefined reference to symbol 'vtkImageAlgorithm::GetOutput()'

I want to build a project using make in Ubuntu. This project includes VTK, Xerces and Cmake libraries. While builing I get the following error:
Linking CXX static library libMA_LaTIM.a
[ 96%] Built target MA_LaTIM
Scanning dependencies of target MA_LaTIM_entrainement
[100%] Building CXX object applications/CMakeFiles/MA_LaTIM_entrainement.dir/entrainement.cxx.o
Linking CXX executable MA_LaTIM_entrainement
/usr/bin/ld: ../algorithmes/libMA_LaTIM.a(LecteurImage.cxx.o): undefined reference to symbol 'vtkImageAlgorithm::GetOutput()'
/usr/bin/ld: note: 'vtkImageAlgorithm::GetOutput()' is defined in DSO /usr/lib/libvtkFiltering.so.5.8 so try adding it to the linker command line
/usr/lib/libvtkFiltering.so.5.8: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
How can I fix this problem? Here is my CMakeLists.txt, and here is my Makefile.
If you happen to not use cmake to build your application, here is the solution to resolve the linker error undefined reference to symbol with VTK!
The libraries have to be chosen manually. The problem is, that e.g. with my version VTK 6.1 there are 377 shared libs to choose from. Well, some are as before with version 5 but split in Core and special libs.
Others can be found if you check the header of the module the compiler complains about: there is a macro between "class" and the class name: "class VTK_......_EXPORT" , which gives you a hint how the lib is named.
E.g. class VTKRENDERINGCORE_EXPORT vtkRenderer needs library libvtkRenderingCore. Here an example (Linux) which worked for my application:
-L/usr/lib64/vtk -lvtkCommonCore -lvtkFiltersCore -lvtkRenderingCore -lvtkImagingCore -lvtkCommonExecutionModel -l vtkCommonDataModel \
-lvtkFiltersSources -lvtkFiltersGeometry -lvtkFiltersGeneral -lvtkIOCore -lvtkIOImage -lvtkRenderingOpenGL

Resources