链接boost与自己的头和源代码

Linking boost with own headers and source code

本文关键字:源代码 自己的 boost 链接      更新时间:2023-10-16

我正在使用boost库和opencv,现在我必须实现我自己的头文件和源代码(除了main.cpp),它们也需要这些库。main.cpp看起来像(只是在原则上):

// STL includes
#include <stdlib.h>
...(some other STL stuff)
// Boost includes
#include <boost/array.hpp>
...(a lot of other boost stuff)
//OpenCV
#include <opencv2/opencv.hpp>
// Own header files
#include <myHeader.hpp>
main() do_some_stuff;

如果我在myStuff.hpp中没有任何与boost相关的内容,则此操作有效。但是如果我在里面添加一些东西(函数描述在myStuff.cpp中),比如:

class aClass{
    public:
        aClass(int);
        void doSomething(boost::shared_ptr<int>);
        void doSomethingElse(cv::Mat);
};

则表示'boost' had not been declared'cv' does not name a type

我想,好吧,我只需要在这个文件中包含头文件,所以我添加了相同的include,但是当它试图链接时它会给出很多错误,比如:

/usr/include/boost/operators.hpp:308:35: error: expected identifier before numeric constant
/usr/include/boost/operators.hpp:308:35: error: expected ‘>’ before numeric constant
/usr/include/boost/operators.hpp:310:1: error: expected class-name before ‘{’ token
/usr/include/boost/operators.hpp:311:3: error: expected unqualified-id before numeric constant
...(a lot more of these errors)

我正在使用Makefile来构建这个项目,它看起来像:

OPENCV_I = `pkg-config --cflags opencv`
#it finds boost without any additional -I or -L options...
INCLUDEPATHS = $(OPENCV_I)
                -I.
                -L.
LIBS=-lGL -lGLU -lm -lboost_program_options  -lboost_system -lboost_thread -pthread -lrt -lopencv_core -lopencv_highgui
SRCCXX := main.cpp myStuff.cpp
OBJSCXX := $(SRCCXX:%.cpp=${BUILDDIR}/%.o)
$(BUILDDIR)/%.o : %.cpp
    $(CXX) $(CXXFLAGS) $(INCLUDEPATHS) -c $< -o $@ -DdDOUBLE $(LIBS)
all: ${OBJSCXX}
    $(CXX) $(LDFLAGS) $(INCLUDEPATHS) -o $(OUTNAME) $?  -DdDOUBLE $(LIBS)

以前我使用CMake,它在这类项目中工作得很好,只是这个是一个更大项目的一部分,他们使用makefile来处理所有事情。所以我想主要问题是与makefile,可能当我列出我的源代码SRCCXX := main.cpp Visualisation.cpp它不喜欢它…有什么建议吗?

提前谢谢你。

编辑 ....................................

所以我的整个mystuff。hpp看起来像:

#define _USE_MATH_DEFINES
#include <math.h>
#define RINGS 5
#define SECTIONS 12
// Boost includes
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/assign/ptr_list_of.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/date_time.hpp>
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <boost/make_shared.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/program_options.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <opencv2/opencv.hpp>
class Sensor{
    public:
        Sensor(int);
        void Update(boost::shared_ptr<char[RINGS][SECTIONS]>);
        int Id();
    private:
        char data[RINGS][SECTIONS];
        int id;
};

和myStuff.cpp:

#include "myStuff.hpp"
void Sensor::Update(boost::shared_ptr< char[RINGS][SECTIONS] > buffer){
    for(int i=0;i<RINGS;i++) for(int j=0;j<SECTIONS;j++) data[i][j]=buffer[i][j];
};
Sensor::Sensor(int a){
    id=0;
};
int Sensor::Id(){
    return id;
};

and my main.cpp:

// STL includes
#include <stdlib.h>
#include <iostream>
#include <mutex>
#include <string.h>
#include <typeinfo>
#include <queue>
#include <memory>
// Boost includes
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/assign/ptr_list_of.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/date_time.hpp>
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <boost/make_shared.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/program_options.hpp>
#include <boost/numeric/ublas/matrix.hpp>
//OpenCV
#include <opencv2/opencv.hpp>
// Own header files
#include "myStuff.hpp"
////////////////////////////////////////// Main
int main (int argc, char **argv)
{   
    Sensor sensor(0);
    return 0;
}

首先…当在项目中包含您自己的头文件时,我建议您使用"mine.hpp"而不是<mine.hpp>。这确保了编译器不会搜索完整的包含路径,并意外地找到一些同名的其他包含(例如不同的版本)。

第二,当你在一个类中有一个依赖时,你要在这个类中包含这个依赖的头文件。你不能假设有人会把你所有的依赖都包含在一个主类或其他类中。有时有人只是想单独使用你的类。你不希望他们不得不找出你的依赖关系。也不用担心复制,因为include保护(或pragma)会阻止复制。

对于你的特殊问题,你需要使用你的代码。在这一点上,您当然已经设法正确地包含了标题。我猜它们可能源于某个地方缺失的{;。看看你的第一个错误,然后解决它。

编辑问题似乎与您如何使用boost有关。我看了看operator .hpp和版本1_44,我看到的是一个具有4个模板参数的结构体定义,其中一个默认为boost::detail::empty_base<T>。我唯一能说的就是确保你的包含路径和链接路径上有你的整个boost库。

EDIT2

从你新发布的代码我看到了几个问题。首先,你有太多的包括在你的标题。你应该只在你的头文件中包含类依赖,并且总是喜欢把你的头文件放在你的实现(.cpp)文件中。这有助于防止编译时间过长。所以首先修改你的头文件,只包含你需要的依赖项:

#include <boost/shared_ptr.hpp>
#define RINGS 5
#define SECTIONS 12
class Sensor{
    public:
        Sensor(int);
        void Update(boost::shared_ptr<char[RINGS][SECTIONS]>);
        int Id();
    private:
        char data[RINGS][SECTIONS];
        int id;
};

然后在你的实现中唯一的变化是在你的for循环周围放上大括号(这是为了清晰和安全…明白为什么你把它放在一行,但这是不值得的)。也把你的CTOR放在首位:

#include "myStuff.hpp"
Sensor::Sensor(int a){
    id=0;
};
void Sensor::Update(boost::shared_ptr< char[RINGS][SECTIONS] > buffer){
    for(int i=0;i<RINGS;i++) {
       for(int j=0;j<SECTIONS;j++) { 
           data[i][j]=buffer[i][j];
       }
    }
};
int Sensor::Id(){
    return id;
};
主要

#include "myStuff.hpp"
int main (int argc, char **argv)
{   
    Sensor sensor(0);
    return 0;
}

你唯一需要的依赖是boost::shared_ptr。说实话,我很确定你也不需要。无论如何,我建议您从上面的开始,然后通过每次添加一个依赖项来构建。

几个小时后我明白了。cpp也需要包含头文件。愚蠢的错误,但为什么编译器不能说一些像未定义或找不到的东西,而不是几页混乱的错误…?

谢谢你的帮助。