0

我正在开发一个项目,用 C++ 编码,以在 Raspberry pi 和 Arduino 之间开发基于 TCP/IP 的通信 Modbus。

我有一个在 Raspberry 中工作的“hello world”的基本示例。我想在我的 Eclipse 项目中添加 libmodbus 库(.h 和 .c 文件),并测试提供的服务器单元测试。我已经在“路径和符号”(GNU C 和 GNU C++)和“包含”中添加了目录。

在 MakeFile 中,我添加了 libmodbus 文件部分的目录(#Include 路径)。当我构建项目时,只编译 .h 文件,没有编译 .c 文件。“modbus.h”被识别,但它们是像“未定义引用....(modbus fonctions)之类的错误。

我已经测试了许多解决方案,但它还不起作用。

提前致谢。

    ################################################################################
# MAKEFILE    : RPi2Test01

################################################################################

#===============================================================================
# User compiling options
#===============================================================================

#HOST: PC, PCLINUX, ARM
HOST := PC
#HOST := PCLINUX
#HOST :: ARM

#TARGET: PC, ARM, RASPBERRYPI
#TARGET := PC
#TARGET := ARM
TARGET := RASPBERRYPI

#DEBUG: YES, NO
DEBUG := NO

#===============================================================================
# Compiling options
#===============================================================================
#-------------------------------------------------------------------------------
# Determine compiling environment #ENVIRONMENT: CYGWIN,LINUX,WCROSS,LCROSS,ARM
#-------------------------------------------------------------------------------

ifeq ($(HOST),PC)
  ifeq ($(TARGET),PC)
    ENVIRONMENT := CYGWIN
  else
    ENVIRONMENT := WCROSS
  endif
else
  ifeq ($(HOST),LINUXPC)
    ifeq ($(TARGET),PC)
      ENVIRONMENT := LINUX
    else
      ENVIRONMENT := LCROSS
    endif
  else
    ifeq ($(HOST),ARM)
      ENVIRONMENT := ARM
    endif
  endif
endif

#-------------------------------------------------------------------------------
# Customize: WIN32 (CYGWIN)
#-------------------------------------------------------------------------------

ifeq ($(ENVIRONMENT),CYGWIN)
  PREFIX    := 
  BINPREFIX := 
  CFLAGS    := -std=gnu99 -Wno-write-strings
  CFLAGS    += -D_WIN32_ -D__CYGWIN__ -D_$(TARGET)_
  CPPFLAGS  := -pipe -g2 -Wall -Wno-write-strings -Wno-reorder
  CPPFLAGS  += -D_WIN32_ -D__CYGWIN__ -D_$(TARGET)_
  LDFLAGS   := -enable-auto-import
  LIBEXT    := .a
  BINEXT    := .exe
endif

#-------------------------------------------------------------------------------
# Customize: LINUX (LINUX)
#-------------------------------------------------------------------------------

ifeq ($(ENVIRONMENT),LINUX)
  PREFIX    := 
  BINPREFIX := 
  CFLAGS    := -std=gnu99 -Wno-write-strings
  CFLAGS    += -D_LINUX_ -D_$(TARGET)_
  CPPFLAGS  := -Wno-write-strings -Wno-reorder
  CPPFLAGS  += -D_LINUX_ -D_$(TARGET)_
  LDFLAGS   := -g
  LIBEXT    := .a
  BINEXT    := .out
endif

#-------------------------------------------------------------------------------
# Customize: Cross-compiling CYGWIN to ARM-LINUX-LINARO (WCROSS)
#-------------------------------------------------------------------------------

ifeq ($(ENVIRONMENT),WCROSS)
  PREFIX    := arm-linux-gnueabihf-
  BINPREFIX := /cygdrive/c/Linaro/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09/bin/
  CFLAGS    := -std=gnu99 -Wno-write-strings
  CFLAGS    += -D_LINUX_ -D_$(TARGET)_
  CPPFLAGS  := -Wno-write-strings -Wno-reorder
  CPPFLAGS  += -D_LINUX_ -D_$(TARGET)_
  LDFLAGS   := -g
  LIBEXT    := .a
  BINEXT    := .out
endif

#-------------------------------------------------------------------------------
# Customize: Cross-compiling LINUX to ARM-LINUX-LINARO (LCROSS)
#-------------------------------------------------------------------------------

ifeq ($(ENVIRONMENT),LCROSS)
  PREFIX    := arm-linux-gnueabihf-
  BINPREFIX := /usr/local/Linaro/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09/bin/
  CFLAGS    := -std=gnu99 -Wno-write-strings
  CFLAGS    += -D_LINUX_ -D_$(TARGET)_
  CPPFLAGS  := -Wno-write-strings -Wno-reorder
  CPPFLAGS  += -D_LINUX_ -D_$(TARGET)_
  LDFLAGS   := -g
  LIBEXT    := .a
  BINEXT    := .out
endif

#-------------------------------------------------------------------------------
# Customize: ARM-LINUX-LINARO (ARM)
#-------------------------------------------------------------------------------

ifeq ($(ENVIRONMENT),ARM)
  PREFIX    := 
  BINPREFIX := 
  CFLAGS    := -std=gnu99 -Wno-write-strings
  CFLAGS    += -D_LINUX_ -D_$(TARGET)_
  CPPFLAGS  := -Wno-write-strings -Wno-reorder
  CPPFLAGS  += -D_LINUX_ -D_$(TARGET)_
  LDFLAGS   := -g
  LIBEXT    := .a
  BINEXT    := .out
endif

#-------------------------------------------------------------------------------
# Compiling and linking features - Paths and libraries
#-------------------------------------------------------------------------------

CINC_PATH := $(INCLUDE)

CLIB_PATH := $(CLIB_PATH)

ifeq ($(ENVIRONMENT),ARM)
  CINC_PATH += -I/usr/local/include
  CLIB_PATH += -L/usr/local/lib
endif

ifeq ($(DEBUG),YES)
  LIBSUBFIX := d
  CPPFLAGS  += -D_DEBUG_
else
  LIBSUBFIX :=
  CPPFLAGS  +=
endif

LDADDLIBS += -lpthread -lrt -lm -lstdc++

#===============================================================================
# Building tools
#===============================================================================

CC   := @$(BINPREFIX)$(PREFIX)gcc
CXX  := @$(BINPREFIX)$(PREFIX)g++
LD   := @$(BINPREFIX)$(PREFIX)ld
AR   := @$(BINPREFIX)$(PREFIX)ar
MD   := @mkdir -p
RM   := @rm -rf
ECHO := @echo

#===============================================================================
# File pathts
#===============================================================================

SRCDIR := .
OBJPATH := ./OBJ
LIBPATH := ./LIB
GENPATH := ./BIN

#===============================================================================
# Source files
#===============================================================================

MAIN_HOME := $(SRCDIR)
CPP_SRCS_MAIN := $(MAIN_HOME)/RPi2Test01.cpp 

#===============================================================================
# Object files
#===============================================================================

OBJS_MAIN := $(patsubst $(MAIN_HOME)/%.cpp, \
               $(OBJPATH)/%.cpp.o, \
               $(CPP_SRCS_MAIN))

#LIB_SRC := C:/libmodbus-3.0.6/src/%.c        

#LIBSMAIN := $(patsubst  C:/libmodbus-3.0.6/src/%.c, \
                  $(LIBPATH)/%.c.o, \
               $(LIB_SRC))


#===============================================================================
# Building options
#===============================================================================

# Include paths
CINC_PATH += -I C:/libmodbus-3.0.6/src
CLIB_PATH += -L C:/libmodbus-3.0.6/src

CLIB_PATH += -L$(LIBPATH) 



# Compiling and Linking options
CFLAGS   := $(CINC_PATH) $(CFLAGS) 
CPPFLAGS := $(CINC_PATH) $(CPPFLAGS)
LDFLAGS  := $(CLIB_PATH) $(LDFLAGS)

#===============================================================================
# Building targets
#===============================================================================

BINMAIN := $(GENPATH)/RPi2Test01$(BINEXT)

#-------------------------------------------------------------------------------
# All targets
#-------------------------------------------------------------------------------
.PHONY: all
all: dirs $(BINMAIN)

#-------------------------------------------------------------------------------
# Building directories
#-------------------------------------------------------------------------------
dirs:
    $(MD) $(OBJPATH)
    $(MD) $(LIBPATH)
    $(MD) $(GENPATH)

#-------------------------------------------------------------------------------
# Targets
#-------------------------------------------------------------------------------

$(BINMAIN): $(OBJS_MAIN) $(LIBSMAIN) 
    $(ECHO) 'Building target: $@'
    $(CC) $(OBJS_MAIN) -o$@ $(LDFLAGS) $(LDADDLIBS) 

#-------------------------------------------------------------------------------
# Compiling
#-------------------------------------------------------------------------------

# File compiling: Main Programs ------------------------------------------------
$(OBJPATH)/%.cpp.o: $(MAIN_HOME)/%.cpp 
    $(ECHO) 'Building file: $<'
    $(CXX) -c $(CPPFLAGS) -o$@ $<
#===============================================================================
# Clean
#===============================================================================
.PHONY: clean
clean:
    $(ECHO) 'Removing...'
    $(RM) ${OBJPATH}/*.o ${OBJPATH}/*.err ${OBJPATH}/*.map
    $(RM) ${GENPATH}/*.o ${GENPATH}/*.out ${GENPATH}/*.exe ${GENPATH}/*.tmp 
    $(RM) ${SRCDIR}/*.a ${OBJPATH}/*.a ${LIBPATH}/*.a ${GENPATH}/*.a
    $(RM) ${SRCDIR}/*.so ${OBJPATH}/*.so ${LIBPATH}/*.so ${GENPATH}/*.so
    $(RM) ${SRCDIR}/*.out.stackdump ${SRCDIR}/*.exe.stackdump 
    $(RM) ${GENPATH}/*.out.stackdump ${GENPATH}/*.exe.stackdump 
    $(RM) ${OBJPATH} ${LIBPATH} ${GENPATH}
    $(RM) ./*.o ./*.out ./*.exe ./*.a ./*.so ./*.err ./*.map
    $(RM) ./*.out.stackdump ./*.exe.stackdump
    $(ECHO) 'Files removed'
4

1 回答 1

0

您可能需要添加-lmodbus到您的LDADDLIBS变量:

LDADDLIBS += -lmodbus -lpthread -lrt -lm -lstdc++
于 2017-05-15T11:59:42.483 回答