gpt4 book ai didi

c++ - 如何构建使用sqlite3的ros节点?

转载 作者:行者123 更新时间:2023-12-02 10:21:14 25 4
gpt4 key购买 nike

我正在尝试在ROS节点中使用sqlite3。我认为ROS取决于sqlite3,因此我的系统上已经有了该库。头文件在/ usr / lib /中。但是,当我使用catkin构建测试时,针对sqlite3.h header 中的函数收到链接器错误。我需要在CMake中针对sqlite3构建什么?

我包含了相关文件,但我认为您可能真正需要查看的唯一文件就是CMakeLists.txt。

我正在使用罗斯旋律。

链接器错误:

/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。 (基于错误输出)似乎没有通过catkin_LIBRARIES链接到SQLite3库,因此您可能必须手动执行此操作:

... 

# 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)

...

关于c++ - 如何构建使用sqlite3的ros节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60064778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com