如何搭建一个使用sqlite3的ros节点?
How to build a ros node that uses sqlite3?
我正在尝试在 ROS 节点中使用 sqlite3。我认为 ROS 依赖于 sqlite3,所以我的系统上已经有了这个库。 header 文件位于 /usr/lib/。但是,当我使用 catkin 构建测试时,我收到针对 sqlite3.h header 中函数的链接器错误。我需要在 CMake 中做什么才能针对 sqlite3 进行构建?
我包含了相关文件,但我认为您可能真正需要查看的唯一文件是 CMakeLists.txt。
我正在使用 ros melodic。
链接器错误:
/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}
)
我在哪里导入 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 文件 (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);
}
}
}
如果您的库依赖于 SQLite3,如错误消息和您的文件命名所建议的那样,您应该使用 find_package(SQLite3)
将此包导入 CMake。它没有出现(基于错误输出)SQLite3 库是通过 catkin_LIBRARIES
链接的,因此您可能必须手动执行此操作:
...
# 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)
...
我正在尝试在 ROS 节点中使用 sqlite3。我认为 ROS 依赖于 sqlite3,所以我的系统上已经有了这个库。 header 文件位于 /usr/lib/。但是,当我使用 catkin 构建测试时,我收到针对 sqlite3.h header 中函数的链接器错误。我需要在 CMake 中做什么才能针对 sqlite3 进行构建?
我包含了相关文件,但我认为您可能真正需要查看的唯一文件是 CMakeLists.txt。
我正在使用 ros melodic。
链接器错误:
/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}
)
我在哪里导入 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 文件 (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);
}
}
}
如果您的库依赖于 SQLite3,如错误消息和您的文件命名所建议的那样,您应该使用 find_package(SQLite3)
将此包导入 CMake。它没有出现(基于错误输出)SQLite3 库是通过 catkin_LIBRARIES
链接的,因此您可能必须手动执行此操作:
...
# 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)
...