So I have a directory that's formatted as follows:
Project:
- src
- a.cpp
- b.cpp
- c.cpp
- include
- h.cpp
- h.hpp
How would I get CMake to include h.hpp in the files in the source folder? I tried doing include_directories(include) but CMake is still unable to find the file. I also tried changing the include directive in a.cpp to #include "../include/h.hpp". However, none of these solutions have worked.
EDIT:
The output is:
[build] Consolidate compiler generated dependencies of target a
[build] Consolidate compiler generated dependencies of target c
[build] Consolidate compiler generated dependencies of target b
[build] [ 16%] Building CXX object CMakeFiles/b.dir/src/b.cpp.o
[build] [ 33%] Building CXX object CMakeFiles/c.dir/src/c.cpp.o
[build] [ 50%] Building CXX object CMakeFiles/a.dir/src/a.cpp.o
[build] [ 66%] Linking CXX executable a
[build] [ 83%] Linking CXX executable c
[build] [100%] Linking CXX executable b
[build] /usr/bin/ld: CMakeFiles/a.dir/src/a.cpp.o: in function `func(...)':
[build] ../a.cpp:55: undefined reference to `func(...)'
[build] /usr/bin/ld: a.cpp:58: undefined reference to `func(...)'
Note that func is a function with an implementation provided in h.cpp.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake
CACHE STRING "Vcpkg toolchain file")
project(proj)
find_package(fmt CONFIG REQUIRED)
find_package(CUDAToolkit REQUIRED)
link_libraries(
fmt::fmt CUDA::nvrtc CUDA::cuda_driver CUDA::cudart
)
include_directories(include)
add_executable(a ${CMAKE_CURRENT_SOURCE_DIR}/src/a.cpp)
add_executable(b ${CMAKE_CURRENT_SOURCE_DIR}/src/b.cpp)
add_executable(c ${CMAKE_CURRENT_SOURCE_DIR}/src/c.cpp)
You should add h.cpp as a source for each executable that uses functions from h.hpp:
add_executable(a ${CMAKE_CURRENT_SOURCE_DIR}/src/a.cpp ${CMAKE_CURRENT_SOURCE_DIR}/include/h.cpp)
add_executable(b ${CMAKE_CURRENT_SOURCE_DIR}/src/b.cpp ${CMAKE_CURRENT_SOURCE_DIR}/include/h.cpp)
add_executable(c ${CMAKE_CURRENT_SOURCE_DIR}/src/c.cpp ${CMAKE_CURRENT_SOURCE_DIR}/include/h.cpp)
As you have said,
func is a function with an implementation provided in h.cpp
so you need to add that implementation to your executables
Related
Trying to get OpenCV included in my C++ project, but having issues for some reason...
I followed the instructions to install from source:
git clone https://github.com/opencv/opencv.git
mkdir build && cd build
cmake ../opencv
make -j4
make install
I can see it in my /usr/local/include under 'opencv4'
In my CMakeLists.txt I have:
add_executable(Nssams main.cpp ${BACKWARD_ENABLE})
add_subdirectory(camera)
add_subdirectory(mqtt_client)
add_subdirectory(jsoncpp)
add_subdirectory(serial)
add_subdirectory(backward-cpp)
if(${BUILD_WITH_TESTING})
add_subdirectory(tests)
endif()
find_package(PahoMqttCpp REQUIRED)
find_package(spdlog REQUIRED)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV LIBS = ${OpenCV_LIBS}")
target_link_libraries(Nssams
mqtt
camera
jsoncpp
serial
PahoMqttCpp::paho-mqttpp3-static
spdlog
${OpenCV_LIBS}
)
The messages are spitting out:
[cmake] -- OpenCV_INCLUDE_DIRS = /usr/local/include/opencv4
[cmake] -- OpenCV LIBS = opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;
But when I try to #include, i.e.
#include "opencv4/opencv2/core.hpp"
It's red underlined and the error says
#include errors detected based on information provided by the configurationProvider setting. Squiggles are disabled for this translation unit (/home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/src/camera/ebcmos.cpp).C/C++(1696)
cannot open source file "opencv2/core/cvdef.h" (dependency of "opencv4/opencv2/core.hpp")C/C++(1696)
Where am I going wrong?
I have done very similar things with the spdlog library that's in the CMakeLists.txt with no issues. Just built it from source, added it to the CMakeLists.txt and could use without issues
EDIT:
CMakeLists.txt build output while having #include "opencv4/opencv2/core/core.hpp" included in a .cpp file
[main] Building folder: nssams-cpp-ebcmos
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/build --config Debug --target all -j 4 --
[build] Consolidate compiler generated dependencies of target backward_object
[build] Consolidate compiler generated dependencies of target camera
[build] Consolidate compiler generated dependencies of target jsoncpp
[build] Consolidate compiler generated dependencies of target mqtt
[build] [ 6%] Built target backward_object
[build] [ 12%] Building CXX object src/camera/CMakeFiles/camera.dir/ebcmos.cpp.o
[build] [ 31%] Built target mqtt
[build] [ 43%] Built target jsoncpp
[build] Consolidate compiler generated dependencies of target serial
[build] Consolidate compiler generated dependencies of target backward
[build] [ 56%] Built target backward
[build] [ 68%] Built target serial
[build] In file included from /home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/src/camera/ebcmos.cpp:8:
[build] /usr/local/include/opencv4/opencv2/core/core.hpp:48:10: fatal error: opencv2/core.hpp: No such file or directory
[build] 48 | #include "opencv2/core.hpp"
[build] | ^~~~~~~~~~~~~~~~~~
[build] compilation terminated.
[build] gmake[2]: *** [src/camera/CMakeFiles/camera.dir/build.make:104: src/camera/CMakeFiles/camera.dir/ebcmos.cpp.o] Error 1
[build] gmake[1]: *** [CMakeFiles/Makefile2:219: src/camera/CMakeFiles/camera.dir/all] Error 2
[build] gmake: *** [Makefile:136: all] Error 2
[proc] The command: /usr/bin/cmake --build /home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/build --config Debug --target all -j 4 -- exited with code: 2 and signal: null
[build] Build finished with exit code 2
It does build correctly if I do not have that include defined
output below:
[main] Building folder: nssams-cpp-ebcmos
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/bwhitely/Projects/NSSAMS/nssams-cpp-ebcmos/build --config Debug --target all -j 4 --
[build] [ 6%] Building CXX object src/camera/CMakeFiles/camera.dir/ebcmos.cpp.o
[build] [ 12%] Built target backward_object
[build] [ 25%] Built target jsoncpp
[build] [ 43%] Built target mqtt
[build] [ 56%] Built target serial
[build] [ 68%] Built target backward
[build] [ 75%] Linking CXX static library ../../lib/libcamera.a
[build] [ 87%] Built target camera
[build] Consolidate compiler generated dependencies of target Nssams
[build] [ 93%] Linking CXX executable ../bin/Nssams
[build] [100%] Built target Nssams
[build] Build finished with exit code 0
Hello and thanks in advance for the help,
I have a linking issue with my cmake that I don't get. I'm trying to include a shared lib(modbuspp) in submodule which require another shared lib(libmodbus) on my computer. It seem simple but during exec linking it give me undefined reference to my computer lib (libmodbus) when I did add the includes. Does anyone has an idea why ? Here is my Cmake:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(This test)
project(${This} VERSION "0.1.0")
configure_file("${PROJECT_SOURCE_DIR}/src/config.h.in" "${PROJECT_SOURCE_DIR}/src/config.h")
#enable_testing()
#add_subdirectory(googletest)
#add_subdirectory(test)
file(GLOB Sources
"src/*.hpp"
"src/*.cpp"
)
add_executable(${This} ${Sources})
find_package(nlohmann_json 3.2.0 REQUIRED)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package(Modbus REQUIRED)
message(TROLOLO:${MODBUS_INCLUDE_DIRS})
include_directories(${MODBUS_INCLUDE_DIRS})
#target_include_directories(${This} PRIVATE ${MODBUS_INCLUDE_DIRS})
#work on libmodbuspp exemple "-shared -fPIC -pthread -I/usr/local/include -I/usr/include/modbus -L/usr/local/lib -lmodbuspp -lmodbus"
#Configure: cmake -DMODBUSPP_USE_EXTERNAL_JSON=ON -DMODBUSPP_WITH_STATIC=1 -DINSTALL_LIB_DIR=libs -DMODBUSPP_UNIT_TESTS=0 -S . -B build
add_subdirectory(external/libmodbuspp)
include_directories(external/libmodbuspp/include)
#target_include_directories(${This} PRIVATE external/libmodbuspp/include)
link_directories(${This} PRIVATE ${PROJECT_BINARY_DIR}/external/libmodbuspp/lib)
target_link_libraries(${This} PRIVATE ${MODBUS_LIBRARIES} modbuspp-shared)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(${This} PUBLIC Threads::Threads)
And here are the compile error:
Scanning dependencies of target objlib
[ 4%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/bufferedslave.cpp.o
[ 9%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/device.cpp.o
[ 14%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/master.cpp.o
[ 19%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/message.cpp.o
[ 23%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/netlayer.cpp.o
[ 28%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/request.cpp.o
[ 33%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/response.cpp.o
[ 38%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/router.cpp.o
[ 42%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/rtulayer.cpp.o
[ 47%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/server.cpp.o
/home/aurelia/git/test/external/libmodbuspp/src/server.cpp: In static member function ‘static void* Modbus::Server::Private::loop(std::future<void>, Modbus::Server::Private*)’:
/home/aurelia/git/test/external/libmodbuspp/src/server.cpp:434:3: warning: no return statement in function returning non-void [-Wreturn-type]
434 | }
| ^
[ 52%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/slave.cpp.o
[ 57%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/tcplayer.cpp.o
[ 61%] Building CXX object external/libmodbuspp/lib/CMakeFiles/objlib.dir/__/src/timeout.cpp.o
[ 61%] Built target objlib
Scanning dependencies of target modbuspp-shared
[ 66%] Linking CXX shared library libmodbuspp.so
[ 66%] Built target modbuspp-shared
Scanning dependencies of target test
[ 71%] Building CXX object CMakeFiles/test.dir/src/StaticDI.cpp.o
[ 76%] Building CXX object CMakeFiles/test.dir/src/StaticDO.cpp.o
[ 80%] Building CXX object CMakeFiles/test.dir/src/conf.cpp.o
[ 85%] Building CXX object CMakeFiles/test.dir/src/main.cpp.o
[ 90%] Building CXX object CMakeFiles/test.dir/src/modbusServer.cpp.o
[ 95%] Linking CXX executable test
/usr/bin/ld: external/libmodbuspp/lib/libmodbuspp.so.1.0.0: undefined reference to `modbus_rtu_set_rts_delay'
/usr/bin/ld: external/libmodbuspp/lib/libmodbuspp.so.1.0.0: undefined reference to `modbus_get_byte_timeout'
/usr/bin/ld: external/libmodbuspp/lib/libmodbuspp.so.1.0.0: undefined reference to `modbus_rtu_get_rts'
/usr/bin/ld: external/libmodbuspp/lib/libmodbuspp.so.1.0.0: undefined reference to `modbus_close'
/usr/bin/ld: external/libmodbuspp/lib/libmodbuspp.so.1.0.0: undefined reference to `modbus_mapping_free'
/usr/bin/ld: external/libmodbuspp/lib/libmodbuspp.so.1.0.0: undefined reference to `modbus_set_indication_time
But all those reference can be found in the include that was linked with include_directories :
/usr/include/modbus$ grep rtu_set_rts_delay ./*
./modbus-rtu.h:MODBUS_API int modbus_rtu_set_rts_delay(modbus_t *ctx, int us);
I have searched various forums and cannot find a solution to my problem.
I'm trying to write unit tests of a function in a C file. I'm using google test library.
Although I follow the guides, the project doesn't compile properly.
Tests written in C ++ do not see functions in C file. (undefined reference)
Below I am attaching my files, maybe someone will notice where I made a mistake. I'm out of ideas.
pir_driver.h
#ifndef PIR_DRIVER_H_
#define PIR_DRIVER_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
uint32_t xTaskGetTickCount();
#ifdef __cplusplus
} /* extern "C" */
#endif
pir_driver.c
#include "pir_driver.h"
uint32_t xTaskGetTickCount()
{
return 1000;
}
pir_test.cpp
#include <gtest/gtest.h>
#include "../pir_driver.h"
TEST(PIR_Timer_Test, Start)
{
uint32_t test = xTaskGetTickCount() * 2;
EXPECT_EQ(test, test);
}
Project root CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(This pir_driver)
project(${This} C CXX)
include(Dart)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
add_subdirectory(googletest)
set(Headers
pir_driver.h
)
set(Source
pir_driver.c
)
add_library(${This} STATIC ${Sources} ${Headers})
set_target_properties(${This} PROPERTIES LINKER_LANGUAGE C)
add_subdirectory(test)
test CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(This pir_test)
set(Sources pir_test.cpp )
add_executable(${This} ${Sources})
set_target_properties(${This} PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(${This} PUBLIC pir_driver gtest_main )
add_test( NAME ${This} COMMAND ${This})
Compile log:
[proc] Wykonywanie polecenia: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/xxx/ansi_c_projects/pir_test/build --config Debug --target all -- -j 10
[build] [ 14%] Linking C static library libpir_driver.a
[build] [ 28%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
[build] [ 28%] Built target pir_driver
[build] [ 42%] Linking CXX static library ..\lib\libgtestd.a
[build] [ 42%] Built target gtest
[build] [ 57%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.obj
[build] [ 71%] Linking CXX static library ..\lib\libgtest_maind.a
[build] [ 71%] Built target gtest_main
[build] [ 85%] Building CXX object test/CMakeFiles/pir_test.dir/pir_test.cpp.obj
[build] [100%] Linking CXX executable pir_test.exe
[build] CMakeFiles\pir_test.dir/objects.a(pir_test.cpp.obj): In function `PIR_Timer_Test_Start_Test::TestBody()':
[build] C:/Users/xxx/ansi_c_projects/pir_test/test/pir_test.cpp:13: undefined reference to `xTaskGetTickCount'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [test\CMakeFiles\pir_test.dir\build.make:108: test/pir_test.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:1004: test/CMakeFiles/pir_test.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:113: all] Error 2
The reason it doesn't work is that you have a typo in the main CMakeLists.txt: You define the variable Source, but use the variable Sources when populating the sources of the pir_driver target. Consequently the .c file isn't compiled and the linker can't find the symbol defined within.
The missing .c file is also the reason why you needed to manually set the linker language in the first place. Once you add the source file you can remove the set_target_properties(${This} PROPERTIES LINKER_LANGUAGE C) line as CMake will figure it out itself based on the extensions of the source files.
To avoid such problems in the future you can use
add_library(pir_driver STATIC)
target_sources(pir_driver
PRIVATE
pir_driver.h
pir_driver.c
)
instead of CMake variables to collect sources and headers in CMake 3.11 and later.
I've been writing a compiler using LLVM as the backend. The CMake files I've written so far have worked on Linux, but I haven't had any luck on Windows. The project is split into a library and "driver" executable with their own CMakeLists.txt in separate subdirectories.
The top level CMakeLists.txt looks like this:
cmake_minimum_required (VERSION 3.7.0)
project (compiler)
set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
find_package (LLVM REQUIRED CONFIG)
message (STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message (STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include_directories (${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_subdirectory (Compiler_Lib)
add_subdirectory (Compiler_exe)
The CMakeLists.txt for the library:
cmake_minimum_required (VERSION 3.7.0)
add_library (compiler_lib
AST.cpp
AST.h
parser.cpp
parser.h
scanner.cpp
scanner.h
token.cpp
token.h
visualizer.cpp
visualizer.h
codegen.cpp
codegen.h)
target_include_directories (compiler_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(compiler_lib LLVM)
And the CMakeLists.txt for the executable (which is where linking to the libraries fails):
cmake_minimum_required (VERSION 3.7.0)
project (compiler_exe)
add_executable (compiler_exe Compiler_exe.cpp getopt.h getopt.cpp)
target_link_libraries (compiler_exe LINK_PUBLIC LLVM compiler_lib)
I run the command "c:\Program Files\CMake\bin\cmake.exe" .. -G"MinGW Makefiles" -DCMAKE_PREFIX_PATH=C:\Users\James\llvm+clang-7.0.0-win64-msvc-release where C:\Users\James\llvm+clang-7.0.0-win64-msvc-release is the path to prebuilt LLVM libraries. However, running mingw32-make afterwards fails with the output
Scanning dependencies of target compiler_lib
[ 10%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/AST.cpp.obj
[ 20%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/parser.cpp.obj
[ 30%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/scanner.cpp.obj
[ 40%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/token.cpp.obj
[ 50%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/visualizer.cpp.obj
[ 60%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/codegen.cpp.obj
[ 70%] Linking CXX static library libcompiler_lib.a
[ 70%] Built target compiler_lib
Scanning dependencies of target compiler_exe
[ 80%] Building CXX object Compiler_exe/CMakeFiles/compiler_exe.dir/Compiler_exe.cpp.obj
[ 90%] Building CXX object Compiler_exe/CMakeFiles/compiler_exe.dir/getopt.cpp.obj
[100%] Linking CXX executable compiler_exe.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lLLVM
collect2.exe: error: ld returned 1 exit status
Compiler_exe\CMakeFiles\compiler_exe.dir\build.make:102: recipe for target 'Compiler_exe/compiler_exe.exe' failed
mingw32-make[2]: *** [Compiler_exe/compiler_exe.exe] Error 1
CMakeFiles\Makefile2:176: recipe for target 'Compiler_exe/CMakeFiles/compiler_exe.dir/all' failed
mingw32-make[1]: *** [Compiler_exe/CMakeFiles/compiler_exe.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
This is the first time I've used CMake so I could have missed something obvious, but as I say it seems to work on Linux.
For the linking to succeed two things need to be true:
1) the file libLLVM.a needs to exist
2) that file has to be in a directory in the library search path
There should be a way to get cmake to tell you the list of places it searches for libraries, and you need to find a way to get wherever libLLVM.a exists into that list of dirs.
Apologies for the partial answer, but that's the troubleshooting path...
I've been trying to 'hack' 2 programs together and one of them (openTLD) uses cmake. I've been reading up and working on this issue for a bit now and can't seem to sort it.
When I 'make' where there is no instantiation of the cpp object it compiles fine, when I have an object (that relies on libusb) that I hacked in I'm getting linking (I think) errors.
My CMakeLists (added bits delimeted by ** or CAPS)
#Set minimum version requered
cmake_minimum_required(VERSION 2.4.6)
#just to avoid the warning
if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)
#set project name
project(TLD)
#Append path to the module path
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../cmake/Modules/")
#OpenCV
find_package(OpenCV REQUIRED)
#** ADDED **
find_package(libusb-1.0 REQUIRED)
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../lib)
#set the include directories **CHANGED** added libusb
include_directories (${PROJECT_SOURCE_DIR}/../include ${LIBUSB_1_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
#** ADDED **
SET(CMAKE_CXX_FLAGS "-lusb-1.0")
#libraries
add_library(tld_utils tld_utils.cpp)
add_library(LKTracker LKTracker.cpp)
add_library(ferNN FerNNClassifier.cpp)
add_library(tld TLD.cpp)
# **CHANGED** THIS CLASS COMPILES
add_library(servo servo.cpp)
#executables
# ** WHEN I TRY TO ISTANTIATE 'SERVO' IN 'tld.cpp' THAT IS ISTANTIATED THIS CLASS I GET PROBLEMS
add_executable(run_tld run_tld.cpp)
#link the libraries **CHANGED** added servo and libusb
target_link_libraries(run_tld tld LKTracker ferNN tld_utils servo ${libusb-1.0__LIBS} ${OpenCV_LIBS})
#set optimization level
set(CMAKE_BUILD_TYPE Release)
The output from terminal is :
lewis#lewis-desktop:~/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/build$ cmake ../src/
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - 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
-- Found libusb-1.0:
-- - Includes: /usr/include/libusb-1.0
-- - Libraries: /usr/lib/x86_64-linux-gnu/libusb-1.0.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/build
lewis#lewis-desktop:~/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/build$ make
Scanning dependencies of target LKTracker
[ 16%] Building CXX object CMakeFiles/LKTracker.dir/LKTracker.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libLKTracker.a
[ 16%] Built target LKTracker
Scanning dependencies of target ferNN
[ 33%] Building CXX object CMakeFiles/ferNN.dir/FerNNClassifier.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libferNN.a
[ 33%] Built target ferNN
Scanning dependencies of target tld_utils
[ 50%] Building CXX object CMakeFiles/tld_utils.dir/tld_utils.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libtld_utils.a
[ 50%] Built target tld_utils
Scanning dependencies of target servo
[ 66%] Building CXX object CMakeFiles/servo.dir/servo.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libservo.a
[ 66%] Built target servo
Scanning dependencies of target tld
[ 83%] Building CXX object CMakeFiles/tld.dir/TLD.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libtld.a
[ 83%] Built target tld
Scanning dependencies of target run_tld
[100%] Building CXX object CMakeFiles/run_tld.dir/run_tld.o
Linking CXX executable /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/bin/run_tld
/home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libservo.a(servo.o): In function `servo::servo()':
servo.cpp:(.text+0xa): undefined reference to `libusb_init'
servo.cpp:(.text+0x1b): undefined reference to `libusb_get_device_list'
/home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libservo.a(servo.o): In function `servo::deviceMatchesVendorProduct(libusb_device*, unsigned short, unsigned short)':
servo.cpp:(.text+0x49): undefined reference to `libusb_get_device_descriptor'
/home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libservo.a(servo.o): In function `servo::setTarget(int)':
servo.cpp:(.text+0xc1): undefined reference to `libusb_get_device_descriptor'
servo.cpp:(.text+0xe3): undefined reference to `libusb_get_device_descriptor'
servo.cpp:(.text+0x105): undefined reference to `libusb_get_device_descriptor'
servo.cpp:(.text+0x143): undefined reference to `libusb_get_device_descriptor'
servo.cpp:(.text+0x170): undefined reference to `libusb_open'
servo.cpp:(.text+0x19c): undefined reference to `libusb_control_transfer'
servo.cpp:(.text+0x1a6): undefined reference to `libusb_close'
servo.cpp:(.text+0x1c2): undefined reference to `libusb_free_device_list'
servo.cpp:(.text+0x1ce): undefined reference to `libusb_exit'
collect2: ld returned 1 exit status
make[2]: *** [/home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/bin/run_tld] Error 1
make[1]: *** [CMakeFiles/run_tld.dir/all] Error 2
make: *** [all] Error 2
My servo.cpp file incase there is an issue with it (I'm not a cpp programmer and it's been hacked together)
#include <iostream>
#include <libusb-1.0/libusb.h>
#include "protocol.h"
#include "servo.h"
using namespace std;
int count=0;
const unsigned short vendorId = 0x1ffb;
unsigned short productIDArray[]={0x0089, 0x008a, 0x008b, 0x008c};
libusb_context *ctx=0;
libusb_device **device_list=0;
servo::servo(){
libusb_init(&ctx);
count=libusb_get_device_list(ctx, &device_list);
}
bool servo::deviceMatchesVendorProduct(libusb_device *device, unsigned short idVendor, unsigned short idProduct)
{
libusb_device_descriptor desc;
libusb_get_device_descriptor(device, &desc);
return idVendor == desc.idVendor && idProduct == desc.idProduct;
}
void servo::setTarget(int position)
{
for(int i=0;i<count;i++)
{
libusb_device *device=device_list[i];
{
for(int Id=0;Id<4;Id++)
{
if(deviceMatchesVendorProduct(device, vendorId, productIDArray[Id]))
{
libusb_device_handle *device_handle;
libusb_open(device, &device_handle);
libusb_control_transfer(device_handle, 0x40, REQUEST_SET_TARGET, position*4, 0, 0, 0, (ushort)5000);
libusb_close(device_handle);
break;
}
}
}
}
libusb_free_device_list(device_list, 0);
libusb_exit(ctx);
}
Thanks for any assistance or pointers.
This is indeed a linker error. It looks like you might have the wrong variable for the libusb-1.0 libraries. Try changing your target_link_libraries command to:
target_link_libraries(run_tld tld LKTracker ferNN tld_utils servo ${LIBUSB_1_LIBRARIES} ${OpenCV_LIBS})
You probably also should remove SET(CMAKE_CXX_FLAGS "-lusb-1.0"), since linking will be handled by the target_link_libraries command.