简体   繁体   English

使库具有多个类,其他预先存在的.a库和其他依赖项

[英]Making library with multiple classes, additional pre-existing .a library and other dependencies

I've done several classes that I'm using in a class called " LidarPathMapping ". 我已经在一个名为“ LidarPathMapping ”的类中使用了几个类。 I've been 2 days looking for a solution to make it possible for a person to include the class in his code and use it. 我已经有2天时间寻找一个解决方案,让一个人可以在他的代码中包含该类并使用它。 I don't want him to need to include the other classes that LidarPathMapping depends on and all the other dependencies with which I compile the drivers with which I test my code. 我不希望他需要包含LidarPathMapping所依赖的其他类以及我编译用于测试代码的驱动程序的所有其他依赖项。 My code also depends on a library called librplidar_sdk.a and another called PIGPIO (it runs in a raspberry pi). 我的代码还依赖于一个名为librplidar_sdk.a的库和另一个名为PIGPIO的库(它在一个覆盆子pi中运行)。 I would like that a person that wants to use my code only needs to 我希望一个想要使用我的代码的人只需要

#include "LidarPathMapping.h"

in his code and compile with something like: 在他的代码中编译,例如:

g++ main.cpp -L. -lLidarPathMapping

I don't mind if the person needs to include the libraries I'm using (see in make file) but needing to include every object of each class I've done would defeat the purpose. 我不介意这个人是否需要包含我正在使用的库(参见make文件),但需要包含我所做的每个类的每个对象都会失败。 I have no preference of method, I just want to simplify things for the person that wants to use it. 我没有方法的偏好,我只是想为想要使用它的人简化事情。 I would appreciate if also someone could tell me how the library would be used by the user: how to include in .cpp file and in the g++ compilation command.Here is the makefile I've done to compile a driver to test my code: 如果有人能告诉我用户如何使用该库,我将不胜感激:如何包含在.cpp文件和g ++编译命令中。这是我编译驱动程序来测试我的代码所做的makefile:

CFLAGS=  -w -Wall

LDIR= /home/pi/Documents/code/cpp/andar/andar/include

LIBS= -lboost_iostreams -lboost_system -lboost_filesystem \
       -lrplidar_sdk -lstdc++ -lpthread -lpigpio -lrt -pthread \
       -lGL -lGLU -lglut

#DEFS = -D USE_OPEN_GL
######################### Objects ###########################

OBJS = objects/LidarPlusServo.o \
        objects/CloudManipulation.o \
        objects/RoverParameters.o \
        objects/csvReader.o \
        objects/Dstar.o \
        objects/LidarPathMapping.o \


######################### Headers ###########################

ANDARHEADERS = andar_include/LidarPlusServo.h \
               andar_include/CloudManipulation.h \
               andar_include/RoverParameters.h \
               csvReader/csvReader.hpp \
               include/Dstar.h \
               andar_include/LidarPathMapping.h \

######################### Source Files ###########################
ANDARSRC = LidarPlusServo.cpp \
               CloudManipulation.cpp \
               RoverParameters.cpp \
               csvReader/csvReader.cpp \
               Dstar.cpp \
               LidarPathMapping.cpp \


######################## Executable ##################################

#Change name here#
#If main file is called "helloWorld.cpp", write "helloWorld"

NAME = driverLidPathMap

$(NAME): $(OBJS) $(NAME).cpp
    sudo g++ -std=c++11 $(CFLAGS) -o $(NAME) $(NAME).cpp $(OBJS) \
                     $(LIBS) -L$(LDIR)

####################### Create Objects ########################
objects/LidarPlusServo.o: LidarPlusServo.cpp $(ANDARHEADERS) 
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) LidarPlusServo.cpp

objects/CloudManipulation.o: CloudManipulation.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) CloudManipulation.cpp

objects/RoverParameters.o: RoverParameters.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) RoverParameters.cpp

objects/csvReader.o: csvReader/csvReader.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c  -o $@ $(CFLAGS) csvReader/csvReader.cpp

objects/Dstar.o: Dstar.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c  -o $@ $(CFLAGS) Dstar.cpp

objects/LidarPathMapping.o: LidarPathMapping.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c  -o $@ $(CFLAGS) LidarPathMapping.cpp

First, here is a useful trick. 首先,这是一个有用的技巧。 You have a separate rule for each object: 每个对象都有一个单独的规则:

objects/LidarPlusServo.o: LidarPlusServo.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) LidarPlusServo.cpp

objects/CloudManipulation.o: CloudManipulation.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) CloudManipulation.cpp

objects/RoverParameters.o: RoverParameters.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) RoverParameters.cpp

...

You are using the automatic variable $@ , but you can also use $< for the first argument in the prerequisite list: 您正在使用自动变量$@ ,但您也可以使用$<作为先决条件列表中的第一个参数:

objects/LidarPlusServo.o: LidarPlusServo.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) $<

objects/CloudManipulation.o: CloudManipulation.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) $<

objects/RoverParameters.o: RoverParameters.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) $<

...

Notice that all of these rules are the same, except for the target and the first prerequisite, which follow a simple rule, objects/foo.o: foo.cpp . 请注意,所有这些规则都是相同的,除了目标和第一个先决条件,它遵循一个简单的规则, objects/foo.o: foo.cpp So you can replace all of these rules with one pattern rule: 因此,您可以使用一个模式规则替换所有这些规则:

objects/%.o: %.cpp $(ANDARHEADERS)
    sudo g++ -std=c++11 -c -o $@ $(CFLAGS) $<

Now for building and using the library. 现在用于构建和使用库。 I don't know enough about the internals of your source files to know exactly what changes are necessary, but I will give a simple example to show the technique. 我对源文件的内部结构知之甚少,无法确切知道哪些更改是必要的,但我将举一个简单的例子来展示该技术。

Suppose I have three source files, ant.cpp , bee.cpp , crocket.cpp , with their respective header files, Each contains some classes, some of which depend on each other. 假设我有三个源文件, ant.cppbee.cppcrocket.cpp及其各自的头文件,每个包含一些类,其中一些依赖于彼此。 Now I want to combine them into a shared library, libInsect.so , with an associated header file, Insect.h , and build an executable bughunt that uses it. 现在我想将它们组合成一个共享库libInsect.so ,它带有一个相关的头文件Insect.h ,并构建一个使用它的可执行文件bughunt

Insect.h is easy: Insect.h很简单:

#include "ant.h"
#include "bee.h"
#include "cricket.h"

Now for the makefile: 现在为makefile:

OBJECTS := ant.o bee.o cricket.o

%.o: %.cpp %.h
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -fPIC -c $< -o $@

libInsect.so: $(OBJECTS)
    $(CXX) -fPIC -shared -o $@ $^

bughunt: bughunt.cpp libInsect.so
    $(CXX) -L. $< -lInsect -o $@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM