在库链接期间未找到引用 我在C++中创建了一个名为LIbPARSE的库,该代码被另一个名为LIPBITMPMSG的库使用。
我一直试图在test.cpp中测试libitcmpmsg,但是当我尝试构建它时,编译器返回以下消息:在库链接期间未找到引用 我在C++中创建了一个名为LIbPARSE的库,该代码被另一个名为LIPBITMPMSG的库使用。,c++,cmake,shared-libraries,C++,Cmake,Shared Libraries,我一直试图在test.cpp中测试libitcmpmsg,但是当我尝试构建它时,编译器返回以下消息: $libpath/libitcmpmsg.so: reference not found to "void MSG_PARSER::WriteBigEndian<unsigned char>(std::vector<unsigned char, std::allocator<unsigned char> &, unsingned char)&quo
$libpath/libitcmpmsg.so: reference not found to "void MSG_PARSER::WriteBigEndian<unsigned char>(std::vector<unsigned char, std::allocator<unsigned char> &, unsingned char)"
$libpath/libitcmpmsg.so: reference not found to "unsigned char* MSG_PARSER::ReadBigEndian<unsigned char>(unsigned char&, unsigned char*, unsigned int&)"
$libpath/libitcmpmsg.so: reference not found to "MSG_PARSER::ReadFixedLengthString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned char*, int, unsigned int&)"
$libpath/libitcmpmsg.so: reference not found to "MSG_PARSER::WriteFixedLengthString(std::vector<unsigned char, std::allocator<unsigned char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)"
$libpath/libitcmpmsg.so:reference not found to“void MSG_PARSER::WriteBigEndian(std::vector您必须在cpp中实例化专门的模板类,或者将模板类体放在标题中:您的模板函数(函数体)是在标题(h)还是正文(cpp)中定义的文件?它们在cpp文件中定义。有关模板函数,请参阅。其中定义了ReadFixedLengthString
和WriteFixedLengthString
可能会有所帮助。ReadFixedLengthString和WriteFixedLengthString是在msg_parser.cpp中定义的。我已将模板函数的位置重新定位到头文件,并且错误与它们已经消失了。不使用模板的函数我也会将它们移动到标题和编译的代码中。为什么我也必须移动非模板函数?“为什么我也必须移动非模板函数?”?“-您不必在头文件中定义非模板函数。您对函数的定义ReadFixedLengthString
和WriteFixedLengthString
可能有其他错误。但由于您没有显示此定义(不是声明),我们只能猜测其推理。我将模板函数体更改为标题,但它不起作用。然而,作为一种尝试,我也尝试将普通函数更改为标题,它起了作用。为什么我还必须将普通函数替换为标题?@AndréMachoski,除了一些小错误之外(忘记保存文件等),我建议您没有使用正确的命令进行链接(可能缺少指定所需所有文件的参数),因此将它们添加到标题中就不需要链接了。
template <class T>
void WriteBigEndian(std::vector<uint8_t>& target, T source);
template <class T>
uint8_t* ReadBigEndian(T &target, uint8_t* source, uint32_t &available);
uint8_t* ReadFixedLengthString(string& target, uint8_t *source, int size, uint32_t &available);
void WriteFixedLengthString(std::vector<uint8_t> &target, const string& source, uint32_t size);
#include "ptcinteraction.h"
#include "msg_parser.h"
#include <stdint.h>
using namespace PTC_INTERACTION;
using namespace MSG_PARSER;
PtcInteraction::PtcInteraction( std::vector<uint8_t> &data )
{
m_msgBuffer.clear();
uint32_t tavailable = static_cast<uint32_t>(data.size());
uint8_t *tsource = &data[0];
uint8_t value = 0;
tsource = ReadFixedLengthString(m_message.railScac, tsource, SCAC_SIZE, tavailable);
tsource = ReadBigEndian<uint8_t>(m_message.sizeOfPromptTxt, tsource, tavailable );
tsource = ReadFixedLengthString(m_message.promptTxt, tsource, m_message.sizeOfPromptTxt, tavailable);
tsource = ReadBigEndian<uint8_t>(m_message.sizeOfKeyPressedTxt, tsource, tavailable );
tsource = ReadFixedLengthString(m_message.keyPressedTxt, tsource, m_message.sizeOfKeyPressedTxt, tavailable);
if((&data[0] + data.size()) == tsource)
{
m_msgBuffer = data;
}
else
{
m_msgBuffer = {};
}
}
PtcInteraction::PtcInteraction( OCCPTCMSG::PtcInteractionT &ptcInteraction)
{
m_msgBuffer.clear();
m_message = ptcInteraction;
WriteFixedLengthString(m_msgBuffer, ptcInteraction.railScac, SCAC_SIZE);
WriteBigEndian<uint8_t>(m_msgBuffer, ptcInteraction.sizeOfPromptTxt );
.
.
.
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "ptcinteraction.h"
#include "occptcmessages_generated.h"
#include "msg_parser.h"
#include <cstdio>
using namespace PTC_INTERACTION;
int main(void)
{
OCCPTCMSG::PtcInteractionT teste;
teste.railScac = "abcd";
teste.promptTxt = "message test";
teste.sizeOfPromptTxt = teste.promptTxt.size();
teste.keyPressedTxt = "test";
teste.sizeOfKeyPressedTxt = teste.keyPressedTxt.size();
PTC_INTERACTION::PtcInteraction ptcInter(teste);
PTC_INTERACTION::PtcInteraction ptcInter2(ptcInter.m_msgBuffer);
if ( (ptcInter.m_message.railScac == ptcInter2.m_message.railScac) &&
(ptcInter.m_message.promptTxt == ptcInter2.m_message.promptTxt) &&
(ptcInter.m_message.sizeOfPromptTxt == ptcInter2.m_message.sizeOfPromptTxt) &&
(ptcInter.m_message.keyPressedTxt == ptcInter2.m_message.keyPressedTxt) &&
(ptcInter.m_message.sizeOfKeyPressedTxt == ptcInter2.m_message.sizeOfKeyPressedTxt) )
{
std::cout << "Serialization and deserialization succeeded" << std::endl;
}
}