This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 1 year ago.
My project looks like this:
include/util/safety.h
include/opcodes/8bit_load_opcodes.h
include/CPU.h
src/util/safety.cpp
src/CPU.cpp
src/main.cpp
src/opcodes/8bit_load_opcodes.cpp
CMakeLists.txt
With the CMake script below
cmake_minimum_required(VERSION 3.17)
project(gameboy)
enable_testing()
FIND_PACKAGE(Boost 1.65.1 COMPONENTS unit_test_framework REQUIRED)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Boost Not found")
endif()
set(CMAKE_CXX_STANDARD 20)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/opcodes)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/util)
ADD_LIBRARY(cpu_util src/util/safety.cpp)
ADD_LIBRARY(cpu src/CPU.cpp src/opcodes/8bit_load_opcodes.cpp)
TARGET_LINK_LIBRARIES(cpu ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(cpu cpu_util)
add_executable(gameboy src/main.cpp)
TARGET_LINK_LIBRARIES(gameboy cpu)
When I run make I get:
Scanning dependencies of target cpu_util
[ 14%] Building CXX object CMakeFiles/cpu_util.dir/src/util/safety.cpp.o
[ 28%] Linking CXX static library libcpu_util.a
[ 28%] Built target cpu_util
Scanning dependencies of target cpu
[ 42%] Building CXX object CMakeFiles/cpu.dir/src/CPU.cpp.o
[ 57%] Building CXX object CMakeFiles/cpu.dir/src/opcodes/8bit_load_opcodes.cpp.o
[ 71%] Linking CXX static library libcpu.a
[ 71%] Built target cpu
Scanning dependencies of target gameboy
[ 85%] Building CXX object CMakeFiles/gameboy.dir/src/main.cpp.o
[100%] Linking CXX executable gameboy
CMakeFiles/gameboy.dir/src/main.cpp.o: In function `main':
/home/cedric/Programming/gameboy/src/main.cpp:6: undefined reference to `unsigned short validate_argument<unsigned short>(unsigned short, unsigned short)'
collect2: error: ld returned 1 exit status
CMakeFiles/gameboy.dir/build.make:106: recipe for target 'gameboy' failed
make[2]: *** [gameboy] Error 1
CMakeFiles/Makefile2:154: recipe for target 'CMakeFiles/gameboy.dir/all' failed
make[1]: *** [CMakeFiles/gameboy.dir/all] Error 2
Makefile:114: recipe for target 'all' failed
make: *** [all] Error 2
Running nm appears to show an empty output:
$ nm libcpu_util.a
safety.cpp.o:
Why aren't there any symbols in libcpu_util and how can I fix this make error?
For reference:
safety.cpp
#include <stdexcept>
#include "util/safety.h"
template<typename T>
T validate_argument(T value, T mask)
{
if((value & mask) != value)
throw std::runtime_error("Value outside of mask!");
return value;
}
safety.h
#ifndef GAMEBOY_SAFETY_H
#define GAMEBOY_SAFETY_H
template<typename T>
T validate_argument(T value, T mask);
#endif //GAMEBOY_SAFETY_H
The safety.h only has one template function, and the definition part is located in safety.cpp, which is wrong. Template functions should be defined in headers, so you get an empty library here. We need to put the definition part in the header file, and remove the library libcpu_util.a
template<typename T>
T validate_argument(T value, T mask);
Related
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 have a linker error when using Boost.Test with precompiled header (PCH) that does not occur without PCH. I use the dynamically linked library as described in Usage variants.
How can I fix the error to use Boost.Test also with PCH?
The problem occurs at least with Fedora and boost 1.73 (has only dynamic libraries) and g++ 10/clang 11.
$ cmake ../ && make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/.../boost_test_pch/build
[ 33%] Building CXX object CMakeFiles/boost_utf_pch.dir/test_driver.cpp.o
[ 66%] Building CXX object CMakeFiles/boost_utf_pch.dir/test.cpp.o
[100%] Linking CXX executable boost_utf_pch
[100%] Built target boost_utf_pch
vs.
$ cmake -DEDA_ENABLE_PCH=TRUE ../ && make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/.../boost_test_pch/build
[ 25%] Building CXX object CMakeFiles/boost_utf_pch.dir/cmake_pch.hxx.gch
[ 50%] Building CXX object CMakeFiles/boost_utf_pch.dir/test_driver.cpp.o
cc1plus: warning: /home/.../boost_test_pch/build/CMakeFiles/boost_utf_pch.dir/cmake_pch.hxx.gch: not used because `BOOST_TEST_DYN_LINK' is defined [-Winvalid-pch]
[ 75%] Building CXX object CMakeFiles/boost_utf_pch.dir/test.cpp.o
[100%] Linking CXX executable boost_utf_pch
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/boost_utf_pch.dir/build.make:138: boost_utf_pch] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/boost_utf_pch.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
I can not do anything with the warning message before ...
Here the playground files:
CMakeLists.txt:
project(boost_utf_pch LANGUAGES CXX)
cmake_minimum_required(VERSION 3.18)
add_executable(${PROJECT_NAME} "")
find_package(Boost 1.73.0 REQUIRED COMPONENTS
unit_test_framework)
target_sources(${PROJECT_NAME} PRIVATE
test_driver.cpp test.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE
Boost::unit_test_framework)
set_source_files_properties(test_driver.cpp
APPEND PROPERTIES COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK")
option(EDA_ENABLE_PCH "Enable PCH" OFF)
if (EDA_ENABLE_PCH)
target_precompile_headers(${PROJECT_NAME} PRIVATE pch.hpp)
endif()
pch.hpp
#pragma once
#include <boost/test/unit_test.hpp>
test.cpp
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE( my_test )
BOOST_AUTO_TEST_CASE( test_case1 )
{
BOOST_TEST_WARN( sizeof(int) < 4U );
}
BOOST_AUTO_TEST_SUITE_END()
test_driver.cpp
#define BOOST_TEST_MODULE "Boost.UTF PCH Test Suite"
#include <boost/test/unit_test.hpp>
Alan Birtles got the hint into the right direction. I was not aware of the influence of the compiler switch BOOST_TEST_DYN_LINK for the single file in the context of PCH here. A definition for all files of the project in the style of:
target_compile_definitions(${PROJECT_NAME} PRIVATE
"BOOST_TEST_DYN_LINK")
incomprehensibly does not solve the problem. Only after setting the property SKIP_PRECOMPILE_HEADERS for driver 'main' it compiles and links as expected:
project(boost_utf_pch LANGUAGES CXX)
cmake_minimum_required(VERSION 3.18)
add_executable(${PROJECT_NAME} "")
find_package(Boost 1.73.0 REQUIRED COMPONENTS
unit_test_framework)
target_sources(${PROJECT_NAME} PRIVATE
test_driver.cpp test.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE
Boost::unit_test_framework)
set_source_files_properties(test_driver.cpp
APPEND PROPERTIES COMPILE_DEFINITIONS "BOOST_TEST_DYN_LINK")
set_source_files_properties(test_driver.cpp
PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
option(EDA_ENABLE_PCH "Enable PCH" ON)
if (EDA_ENABLE_PCH)
target_precompile_headers(${PROJECT_NAME} PRIVATE pch.hpp)
endif()
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 have a very small project to which I created some unit testing with GTest.
To build it, I'm using CMake. It compiles just fine, but it's giving me the error shown below. I include all make output just in case.
Scanning dependencies of target trace
[ 33%] Building C object CMakeFiles/trace.dir/trace/system_trace.c.o
[ 33%] Built target trace
Scanning dependencies of target data-test
[ 66%] Building CXX object CMakeFiles/data- test.dir/data_testsuite.cpp.o
[100%] Linking CXX executable data-test
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
CMakeFiles/data-test.dir/build.make:86: recipe for target 'data-test' failed
make[2]: *** [data-test] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/data-test.dir/all' failed
make[1]: *** [CMakeFiles/data-test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And I have no idea why is not finding main. Main is part of GTest which I'm including as it can be seen on the CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_STANDARD 11)
project(data-test C CXX)
set(CMAKE_C_FLAGS_DEBUG "-g3 -Og -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -Og -Wall")
set(CMAKE_C_FLAGS_RELEASE "-g0 -O3 -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-g0 -O3 -Wall")
include(GoogleTest)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
include_directories(cfg)
include_directories(system)
include_directories(system/include)
include(trace/CMakeLists.txt)
add_executable(data-test
$<TARGET_OBJECTS:trace>
data_testsuite.cpp
)
target_link_libraries(data-test Threads::Threads GTest::GTest)
gtest_discover_tests(data-test)
And as you can see from the make command output, it is compiling just fine.
Do you know what is going on?
The libraries defined by the target GTest::Gtest doesn't contain main function definition. For obtain definition of main you need link with GTest::Main. This is written in documentation.
target_link_libraries(data-test Threads::Threads GTest::Main)
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.