I am trying to use sqlite3 in a ROS node. I think that ROS depends on sqlite3 so I already have the library on my system. The header file is in /usr/lib/. However, when I build my tests with catkin, I get linker errors against the functions in the sqlite3.h header. What do I need to do in my CMake to build against sqlite3?
I included relevant files, but I think the only one you might really need to see is the CMakeLists.txt.
I'm using ros melodic.
Linker errors:
/home/linuxbrew/.linuxbrew/Cellar/cmake/3.15.1/bin/cmake -E cmake_link_script CMakeFiles/sqlite_gtest.dir/link.txt --verbose=1
/usr/bin/c++ -rdynamic CMakeFiles/sqlite_gtest.dir/test/sqlite_test.cpp.o -o /home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/tros_logging/sqlite_gtest -L/home/alexmussell/catkin_ws/build/tros_logging/gtest -Wl,-rpath,/home/alexmussell/catkin_ws/build/tros_logging/gtest:/home/alexmussell/catkin_ws/build/tros_logging/gtest/googlemock/gtest:/opt/ros/melodic/lib:/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib gtest/googlemock/gtest/libgtest.so /opt/ros/melodic/lib/libroscpp.so -lboost_filesystem -lboost_signals /opt/ros/melodic/lib/librosconsole.so /opt/ros/melodic/lib/librosconsole_log4cxx.so /opt/ros/melodic/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/melodic/lib/libroscpp_serialization.so /opt/ros/melodic/lib/libxmlrpcpp.so /opt/ros/melodic/lib/librostime.so /opt/ros/melodic/lib/libcpp_common.so /usr/lib/x86_64-linux-gnu/libconsole_bridge.so.0.4 -lboost_system -lboost_thread -lboost_chrono -lboost_date_time -lboost_atomic -lpthread /home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so -lpthread
/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so: undefined reference to `sqlite3_step'
/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so: undefined reference to `sqlite3_open'
/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so: undefined reference to `sqlite3_prepare_v2'
/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so: undefined reference to `sqlite3_finalize'
/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so: undefined reference to `sqlite3_bind_text'
/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so: undefined reference to `sqlite3_bind_int64'
/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so: undefined reference to `sqlite3_close'
/home/alexmussell/catkin_ws/devel/.private/tros_logging/lib/libtros_logging.so: undefined reference to `sqlite3_errmsg'
collect2: error: ld returned 1 exit status
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(tros_logging)
find_package(catkin REQUIRED COMPONENTS
roscpp
rostest
)
catkin_package(
INCLUDE_DIRS
include
LIBRARIES
${PROJECT_NAME}
CATKIN_DEPENDS
roscpp
)
set(${PROJECT_NAME}_SRCS
src/sqlite_logging_database.cpp
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_library(${PROJECT_NAME} ${${PROJECT_NAME}_SRCS})
#############
## Testing ##
#############
catkin_add_gtest(sqlite_gtest
test/sqlite_test.cpp
)
target_link_libraries(sqlite_gtest
${catkin_LIBRARIES}
${PROJECT_NAME}
)
Where I import the header (sqlite_logging_database.h)
#pragma once
#include "tros_logging/logging_database.h"
#include <sqlite3.h>
namespace tros_logging
{
class SQLiteLoggingDatabase : public LoggingDatabase
{
public:
SQLiteLoggingDatabase(std::string sqlite_database_file);
~SQLiteLoggingDatabase();
bool logEvent(Event event);
bool logError(Error error);
bool logTiming(Timing timing);
bool logMetric(Metric metric);
Event loadEvent(std::string uuid);
Error loadError(std::string uuid);
Timing loadTiming(std::string uuid);
Metric loadMetric(std::string uuid);
private:
sqlite3* db_connection;
void checkError(int error_code);
};
class SQLiteError : public std::runtime_error
{
public:
explicit SQLiteError(std::string what) : std::runtime_error(what)
{
}
};
}
CPP File that implements my code (sqlite_logging_database.cpp)
#include "tros_logging/sqlite_logging_database.h"
#include <ros/ros.h>
namespace tros_logging
{
SQLiteLoggingDatabase::SQLiteLoggingDatabase(std::string sqlite_database_file)
{
int err = 0;
err = sqlite3_open(sqlite_database_file.c_str(), &(this->db_connection));
if(err)
{
ROS_ERROR_STREAM("Error opening sqlite database file: " << sqlite_database_file << " : " << sqlite3_errmsg(this->db_connection));
}
}
SQLiteLoggingDatabase::~SQLiteLoggingDatabase()
{
sqlite3_close(this->db_connection);
}
bool SQLiteLoggingDatabase::logEvent(Event event)
{
sqlite3_stmt * stmt;
std::string sql = "INSERT INTO events (uuid, type, parent_event_uuid, timestamp) VALUES (?, ?, ?, ?)";
try
{
int err = sqlite3_prepare_v2(this->db_connection, sql.c_str(), -1, &stmt, NULL);
this->checkError(err);
err = sqlite3_bind_text(stmt, 1, event.uuid.c_str(), event.uuid.length(), SQLITE_TRANSIENT);
this->checkError(err);
err = sqlite3_bind_text(stmt, 2, event.type.c_str(), event.type.length(), SQLITE_TRANSIENT);
this->checkError(err);
err = sqlite3_bind_text(stmt, 3, event.parent_uuid.c_str(), event.parent_uuid.length(), SQLITE_TRANSIENT);
this->checkError(err);
err = sqlite3_bind_int64(stmt, 4, event.timestamp.toNSec());
this->checkError(err);
err = sqlite3_step(stmt);
this->checkError(err);
err = sqlite3_finalize(stmt);
this->checkError(err);
}
catch(const SQLiteError e)
{
ROS_ERROR_STREAM(e.what() << '\n');
}
}
bool SQLiteLoggingDatabase::logError(Error error)
{
}
bool SQLiteLoggingDatabase::logTiming(Timing timing)
{
}
bool SQLiteLoggingDatabase::logMetric(Metric metric)
{
}
Event SQLiteLoggingDatabase::loadEvent(std::string uuid)
{
}
Error SQLiteLoggingDatabase::loadError(std::string uuid)
{
}
Timing SQLiteLoggingDatabase::loadTiming(std::string uuid)
{
}
Metric SQLiteLoggingDatabase::loadMetric(std::string uuid)
{
}
void SQLiteLoggingDatabase::checkError(int error_code)
{
if(error_code)
{
std::string err_msg = sqlite3_errmsg(this->db_connection);
ROS_ERROR_STREAM("SQLITE ERROR: " << err_msg);
throw SQLiteError(err_msg);
}
}
}
If your library depends on SQLite3, as the error messages and your file naming suggest, you should import this package into CMake using find_package(SQLite3)
. It doesn't appear (based on the error output) that the SQLite3 library was linked via catkin_LIBRARIES
, so you may have to do this manually:
...
# Tell CMake to locate the SQLite3 package on your machine.
find_package(SQLite3 REQUIRED)
add_library(${PROJECT_NAME} ${${PROJECT_NAME}_SRCS})
# Link the SQLite3 imported target to your own library.
target_link_libraries(${PROJECT_NAME} PUBLIC SQLite::SQLite3)
...