简体   繁体   English

在C语言中,如何添加第三方库并使用Makefile从我现有的代码库中调用

[英]In C, how to add 3rd party Libraries and call from my existing code base using Makefile

I am new to IOT and cloud. 我是物联网和云新手。

For IOT project, we are trying to send on the fly data to cloud for Analytics. 对于物联网项目,我们正在尝试将实时数据发送到云以进行分析。 We are using only c, cpp code in my target board. 我们在目标板上仅使用c,cpp代码。 Debian OS. Debian作业系统。

Every one second, 2 seconds, 3 seconds different types of data will come. 每1秒,2秒,3秒就会出现不同类型的数据。

In my target board, I want to use 3rd party libraries( https_client ) to send data to the cloud. 在目标板上,我想使用第三方库( https_client )将数据发送到云。 I need to hit the service(URL) in the cloud with data (using only GET method). 我需要使用数据(仅使用GET方法)访问云中的service(URL)。

For this, I downloaded https_client code and trying to integrate into my code base. 为此,我下载了https_client代码并尝试将其集成到我的代码库中。

In the below 1st make file is original Makefile, 2nd one is only 3rd party s/w Makefile, the 3rd one is modified original Makefile to integrate 3rd party library. 下面的第一个make文件是原始的Makefile,第二个仅仅是第三方的Makefile,第三个是修改后的原始Makefile集成了第三者库。

In my target board, we don't have much space, so what is best light weight approach to send data to the cloud? 在我的目标板上,我们没有太多空间,那么什么是将数据发送到云的最佳轻量级方法呢? My cloud will support only https and WSS. 我的云将仅支持https和WSS。 Is my approach is correct? 我的方法正确吗? Any other easy way is available? 还有其他简单的方法吗?

In the modified Makefile I am getting below error. 在修改后的Makefile中,我得到以下错误。

make: *** No rule to make target 使:***没有规则成为目标
`https_client-master/https_client-master/main.c', needed by `all'. `all'所需的`https_client-master / https_client-master / main.c'。
Stop. 停止。

My changes in Makefile is not the standard way. 我在Makefile中所做的更改不是标准方法。 Can anyone review my modified Makefile and correct me. 谁能审查我修改过的Makefile并纠正我。

Waiting for your valuable suggestion. 等待您的宝贵建议。

#---------------------------------------------------------------------------
-
--
#1.  Below is the original make file of my existing code base
#---------------------------------------------------------------------------
---
SOURCES=./SRC/MMC/Main.cpp \
    ./SRC/MMC/ModbusMasterController.cpp \
    ./SRC/DM/DeviceManager.cpp \
    .......
    ./Wrapper/MtrWrapper.cpp 


OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=BusMaster
CFLAGS = -c -w -I/usr/include/libxml2 -I./INC/Common -I./INC/ADM -I./INC/MMC 
-I./INC/DM  -I./INC/CM  -I./INC/MMT -I./INC/MPI -I./INC/MMT/DataProc -
I./INC/MMT/CmdProc -I./INC/MMT/CmdProc/Fileoperations -I./INC/SH -
I./INC/FT_MBUS -I./INC/Timer -I./Wrapper -I./INC/DC -Wall

LDFLAGS = -Bdynamic -lSharedMemory -lMessageQueue -luspepc -lxml2 -
lmbusmaster -lrt

CC=g++


all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $@ $(LDFLAGS) 

.cpp.o:
$(CC) $(CFLAGS) $< -o $@

clean: 
rm -rf $(OBJECTS) $(EXECUTABLE)

#----------------------------------------original make end------------------
 -
-------------------

#----------------------------------------2. Below is the 3rd pary Library 
Makefile I want to add--------------------------------------
#CROSS = arm-linux-gnueabihf-

MAKE = make

CC = $(CROSS)gcc
LD = $(CROSS)ld
STRIP = $(CROSS)strip

ROOT_DIR = $(CURDIR)
MBEDTLS = $(ROOT_DIR)/mbedtls

CFLAGS = -fPIC -DHAVE_CONFIG_H -D_U_="__attribute__((unused))" -O2
LDFLAGS =

INCLUDES = -I$(MBEDTLS)/include
LIBS = $(MBEDTLS)/library/libmbedx509.a $(MBEDTLS)/library/libmbedtls.a 
$(MBEDTLS)/library/libmbedcrypto.a

SOURCES = main.c https.c

OBJS = $(SOURCES:.c=.o)

.SUFFIXES:.c .o

all: https_client

mbedtls_make:
@for dir in $(MBEDTLS); do \
    $(MAKE) -C $$dir ; \
    if [ $$? != 0 ]; then exit 1; fi; \
done

https_client: mbedtls_make $(OBJS)
@echo Linking: $@ ....
$(CC) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)
$(STRIP) -s $@

.c.o:
@echo Compiling: $< ....
$(CC) -c $(CFLAGS) $(INCLUDES) -o $@ $<

clean: mbedtls_clean
rm -f https_client *.o

mbedtls_clean:
 @for dir in $(MBEDTLS); do \
    $(MAKE) -C $$dir clean; \
    if [ $$? != 0 ]; then exit 1; fi; \
 done
#----------------------------------------3rd pary Library Makefile end------
  -
-------------------------------

#----------------------------------------3. Modified original Makefile to 
add 
3rd party library----------------

SOURCES=./SRC/MMC/Main.cpp \
    ./SRC/MMC/ModbusMasterController.cpp \
    ./SRC/DM/DeviceManager.cpp \
    .......
    ./Wrapper/MtrWrapper.cpp 

 SOURCES2=./https_client-master/https_client-master/main.c \
    ./https_client-master/https_client-master/https.c

OBJECTS=$(SOURCES1:.cpp=.o)
OBJS=$(SOURCES2:.c=.o)
.SUFFIXES:.c .o
ROOT_DIR = $(CURDIR)
MBEDTLS = $(ROOT_DIR)/https_client-master/https_client-master/mbedtls
EXECUTABLE1=BusMaster
EXECUTABLE2=https_client
CFLAGS = -c -w -I/usr/include/libxml2 -I./INC/Common -I./INC/ADM -I./INC/MMC 
-I./INC/DM  -I./INC/CM  -I./INC/MMT -I./INC/MPI -I./INC/MMT/DataProc -
I./INC/MMT/CmdProc -I./INC/MMT/CmdProc/Fileoperations -I./INC/SH -
I./INC/FT_MBUS -I./INC/Timer -I./Wrapper -I./INC/DC -Wall
CFLAGS2 = -fPIC -DHAVE_CONFIG_H -D_U_="__attribute__((unused))" -O2
INCLUDES = -I$(MBEDTLS)/include
LIBS = $(MBEDTLS)/library/libmbedx509.a $(MBEDTLS)/library/libmbedtls.a 
$(MBEDTLS)/library/libmbedcrypto.a
LDFLAGS = -Bdynamic -lSharedMemory -lMessageQueue -luspepc -lxml2 -
lmbusmaster -lrt
LDFLAGS2 =
STRIP = strip

CC1=g++
CC2=gcc



 all: $(SOURCES1) $(EXECUTABLE1) $(SOURCES2) $(EXECUTABLE2)
 #$(SOURCES2)
 $(EXECUTABLE1): $(OBJECTS)
 @echo EXECUTABLE1:
 $(CC1) $(OBJECTS) -o $@ $(LDFLAGS)

 mbedtls_make:
 @echo mbedtls_make: ....
 @for dir in $(MBEDTLS); do \
    $(MAKE) -C $$dir ; \
    if [ $$? != 0 ]; then exit 1; fi; \
 done

 $(EXECUTABLE2): mbedtls_make $(OBJS)
 @echo Linking: $@ ....
 $(CC1) -o $@ $(OBJS) $(LDFLAGS2) $(LIBS)
 $(STRIP) -s $@

 .c.o:
 @echo Compiling c: $< ....
 $(CC2) -c $(CFLAGS2) $(INCLUDES) -o $@ $<

 .cpp.o:
 @echo Compiling cpp: $< ....
 $(CC1) $(CFLAGS) $< -o $@

 clean: mbedtls_clean
 rm -rf $(OBJECTS) $(EXECUTABLE1)
 rm -f https_client *.o
 mbedtls_clean:
 @for dir in $(MBEDTLS); do \
    $(MAKE) -C $$dir clean; \
    if [ $$? != 0 ]; then exit 1; fi; \
 done


 #------------------------------------------Modified original Makefile end--
 -
 ---------------------------------

I don't think you should have 2 lists of sources. 我认为您不应有2个来源清单。 Below is snippet of how MBEDTLS is included in Makefile. 以下是MBEDTLS如何包含在Makefile中的摘要。

https://github.com/acornblue/AZURE_LAB https://github.com/acornblue/AZURE_LAB

#####################################################
# Projects/Multi/Applications/Azure_Sns_DM/Makefile
#####################################################
C_SOURCE = \
...
Middlewares/Third_Parties/mbedtls/library/aes.c \
Middlewares/Third_Parties/mbedtls/library/aesni.c \
...
-DUSE_MBED_TLS \
-DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'
...
-IMiddlewares/Third_Parties/mbedTLS/include \

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

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