链接boost与自己的头和源代码
Linking boost with own headers and source code
我正在使用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也需要包含头文件。愚蠢的错误,但为什么编译器不能说一些像未定义或找不到的东西,而不是几页混乱的错误…?
谢谢你的帮助。
- 没有为自己的结构调用列表推回方法
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- C++从对象自己的类中删除对象
- 使用 std::optional,而不是自己的结构
- 子轴围绕父轴而不是他自己的轴旋转
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- C++ 如何为自己的迭代器类从迭代器转换为const_iterator?
- 重载 + 自己的类和 std::string 的运算符
- 类无法访问自己的私有静态 constexpr 方法 - Clang bug?
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 如何访问模板参数自己的模板参数?
- 将矩阵乘以我自己的输入的向量
- 您应该在什么时候创建自己的异常类型
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- 如何使用我构建的库,而不会从源代码出错,但不为我自己的项目编译?
- 如何将另一个项目中用 C 编写的源代码包含在我自己的项目中,C++在 Visual Studio 中
- 我可以使用clang解析C代码,即使我必须在我自己的源代码上使用RTTI
- 链接boost与自己的头和源代码