I am trying to create a CANInterface library using PCANBasic. The library is build with CMake whithout indicating problems. When I try to compile (g++ -o Testing Testing.cpp -L/home/gary/Desktop/VSCode/Testing -lCANInterface) a testproram, which is using libCANInterface.a, I get
/usr/bin/ld: /home/gary/Desktop/VSCode/Testing/libCANInterface.a(CANInterface.cpp.o): in function `CAN::InitPCAN()':
/home/gary/Desktop/VSCode/CANInterface/CANInterface.cpp:154: undefined reference to `CAN_Initialize'
(and similar for other used functions from PCANBasic)
CMakeList.txt (for generating libCANInterface.a)
cmake_minimum_required(VERSION 3.10)
project(CANInterface)
add_library(CANInterface CANInterface.cpp)
find_library(lib_path pcanbasic)
target_link_libraries(CANInterface "${lib_path}")
find_path(header_path CANInterface)
target_include_directories(CANInterface PUBLIC "${header_path}")
sample of usage of functions CANInterface.cpp
#include "PCANBasic.h"
...
void CAN::InitPCAN() {
// The Plug & Play Channel (PCAN-USB) is initialized
result = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K);
if (result != PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
CAN_GetErrorText(result, 0, strMsg);
printf("strMsg %s \n", strMsg);
}
else
{
printf("PCAN-USB was initialized \n");
bInitialized = true;
}
}
Testing.cpp
#include <iostream>
#include "CANInterface.h"
int main() {
vehicle vVehicle;
vVehicle.read();
return 0;
}