通过Makefile编译和使用外部链接库时程序不链接

时间:2015-07-20 16:06:59

标签: c++ makefile

我有一个makefile我用来编译我的共享库,它从不同的文件夹中读取文件,然后在自己的文件夹中构建对象和二进制文件。我使用的文件夹结构大致显示在下面。

-Folder
 |
 +--API Header Files Dir
 |
 +--Library (.so) Project One
 |  +-- MakeFile Project One
 |  +-- src (*.cpp files)
 |  +-- inc (*.h files)
 |  +-- obj (compiled object files)
 |  +-- bin (compiled project)
 |
 +--Library (.so) Project Two
 |  +-- MakeFile Project Two
 |  +-- src (*.cpp files)
 |  +-- inc (*.h files)
 |  +-- obj (compiled object files)
 |  +-- bin (compiled project)
 |

我试图组建一个可以完成此任务的makefile将军post非常好,并且我已经将我原来的makefile编辑为最佳我的理解是试图让这项工作。

一个问题 - 虽然我只是在编译源代码时工作正常,但我一直试图在我链接到某些外部库文件的项目中使用它而不是更长的作品。我一直收到以下错误:

collect2: ld returned 1 exit status
make: *** [bin/libTest_v1_5.o] Error 1

我设法在Code :: Blocks中设置一个项目来测试所有项目文件并编译好,所以它只是我的makefile不正确。我想我已经犯了一条错误的规则。

这是我的档案:

######################################################################################
# Purpose               :   MakeFile for building shared library & coupled projects
#
# Date Created          :   25-06-2015
# Date Modified         :   
#
# Note                  : 1. make         : to build unoptimized standard build
#                         2. make debug   : to build  with debug symbols
#                         3. make release : to build  with symbols stripped and optimized
#
# Usage                 : change the "TARGET" to your program name or else use as it is
######################################################################################
ifndef _ARCH
_ARCH := $(shell uname -m)
export _ARCH
endif # wanted to use this in my build directories but figured it's not necessary?

NAME := Test
MAJOR := 1
MINOR := 5
VERSION := v$(MAJOR)_$(MINOR)
TARGET := lib$(NAME)_$(VERSION).o

SRCEXT   := cpp
SRCDIR   := src
INCPATH  := inc
OBJDIR   := obj
BINDIR   := bin
BIN_DEBUGDIR   := $(BINDIR)/debug
API_DIR := /home/API/Multibody/Coupling


INCLUDES := -I. \
            -I$(INCPATH) \
            -I$(API_DIR)

# C++ compiler flag
CXXFLAGS := -Wall -W -march=native -c $(INCLUDES)

# Linker parameter flags
LDFLAGS  := -L$(API_DIR)/lib 
# Linker library flags
LDLIBS   += -lCouplingClientV2_2_0


SRCS    := $(shell find $(SRCDIR) -name '*.$(SRCEXT)')
SRCDIRS := $(shell find . -name '*.$(SRCEXT)' -exec dirname {} \; | uniq)
OBJS    := $(patsubst %.$(SRCEXT),$(OBJDIR)/%.o,$(SRCS))


###########################################################################
# Rules to compile our files  - Do not change below this line!
###########################################################################

ifeq ($(SRCEXT), cpp)
CC       = $(CXX)
else
CXXFLAGS  += -std=gnu99
endif

.PHONY: all debug release clean distclean 


all: $(BINDIR)/$(TARGET)

# Build debug library
debug: CXXFLAGS += -g
debug: clean $(BIN_DEBUGDIR)/$(TARGET)


# strip all symbols from release verison
release: LDFLAGS += -s 
release: CXXFLAGS += -O2
release: $(BINDIR)/$(TARGET)

$(BINDIR)/$(TARGET): buildrepo $(OBJS)
    @mkdir -p `dirname $@`
    @echo "Linking $@..."
    @$(CC) $(OBJS) $(LDFLAGS) -o $@
    @echo "$@ sucessfully built."

$(BIN_DEBUGDIR)/$(TARGET): buildrepo $(OBJS)
    @mkdir -p `dirname $@`
    @echo "Linking $@..."
    @$(CC) $(OBJS) $(LDFLAGS) -o $@ $(LDLIBS) 
    @echo "$@ sucessfully built."

$(OBJDIR)/%.o: %.$(SRCEXT)
    @echo "Generating dependencies for $<..."
    @$(call make-depend,$<,$@,$(subst .o,.d,$@))
    @echo "Compiling $<..."
    @$(CC) $(CXXFLAGS) $< -c -o $@

clean:
    $(RM) -r $(OBJDIR)

distclean: clean
    $(RM) -r $(BINDIR)/$(TARGET)
    $(RM) -r $(BIN_DEBUGDIR)/$(TARGET)

buildrepo:
    @$(call make-repo)

define make-repo
   for dir in $(SRCDIRS); \
   do \
    mkdir -p $(OBJDIR)/$$dir; \
   done
endef


# usage: $(call make-depend,source-file,object-file,depend-file)
define make-depend
  $(CC) -MM       \
        -MMD      \
        -MF $3    \
        -MP       \
        -MT $2    \
        $(CXXFLAGS) \
        $(LDFLAGS) \
        $1
endef

关于如何改进makefile的任何评论都会很棒。任何关于我在链接中做错的答案都会更好!!

更新

这里是来自完全相同的源代码和库的成功编译项目的代码:: blocks的输出。我的问题只与makefile有关 - 而不是在项目源代码或API文件中的任何地方定义的内容。

-------------- Build: Release in Test (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions  -O2 -Wfatal-errors -Wall   -I../Multibody/EDEMCoupling -c /home/API/Multibody/EPT/src/EPT_Consolidation_Damped_v11.cpp -o obj/Release/Multibody/EPT/src/EPT_Consolidation_Damped_v11.o

g++ -L../Multibody/EDEMCoupling/lib   -o bin/Release/EPT_Test obj/Release/Multibody/EPT/src/EPT_Consolidation_Damped_v11.o   -s  /home/API/Multibody/EDEMCoupling/lib/libEDEMCouplingClientV2_2_0.so 
Output size is 48.21 KB
Process terminated with status 0 (0 minutes, 6 seconds)

Makefile输出:

调用make后的终端输出。它永远不会超过从source.cpp编译目标文件的阶段(这是一个非常简单的项目,其中只有一个cpp文件)。我真的不明白为什么因为这个文件适用于所有编译共享库的项目 - 我刚从标志中删除了-shared和-fPIC。

*Generating dependencies for src/EPT_Consolidation_Damped_v11.cpp...
Compiling src/EPT_Consolidation_Damped_v11.cpp...
src/EPT_Consolidation_Damped_v11.cpp: In function ‘int main()’:
src/EPT_Consolidation_Damped_v11.cpp:199: warning: deprecated conversion from string constant to ‘char*’
/home/API/Multibody/EDEMCoupling/ApiTypes.h: At global scope:
/home/API/Multibody/EDEMCoupling/ApiTypes.h:193: warning: ‘const char* NApi::delim()’ defined but not used
Linking bin/Test_v1_5.o...
obj/src/EPT_Consolidation_Damped_v11.o: In function `main':
EPT_Consolidation_Damped_v11.cpp:(.text+0x546): undefined reference to `NApiEDEM::IEDEMCoupling::IEDEMCoupling()'
EPT_Consolidation_Damped_v11.cpp:(.text+0x555): undefined reference to `NApiEDEM::IEDEMCoupling::initialiseCoupling()'
EPT_Consolidation_Damped_v11.cpp:(.text+0x5e3): undefined reference to `NApiEDEM::IEDEMCoupling::connectCoupling(bool, char*)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x6e0): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryId(char const*, int&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x71b): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryId(char const*, int&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x756): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryId(char const*, int&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x16c4): undefined reference to `NApiEDEM::IEDEMCoupling::setGridCellSize(double const&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x16e9): undefined reference to `NApiEDEM::IEDEMCoupling::setNumberOfCores(int const&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x1702): undefined reference to `NApiEDEM::IEDEMCoupling::getEDEMTimeStep(double&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x1725): undefined reference to `NApiEDEM::IEDEMCoupling::getNumberOfTimesteps(unsigned int&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x175c): undefined reference to `NApiEDEM::IEDEMCoupling::getEDEMTime(double&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x17ef): undefined reference to `NApiEDEM::IEDEMCoupling::getEDEMTimesteps(double*, unsigned int&, unsigned int)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x1d66): undefined reference to `NApiEDEM::IEDEMCoupling::setEDEMTime(double const&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x1d7f): undefined reference to `NApiEDEM::IEDEMCoupling::getEDEMTime(double&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x1eb7): undefined reference to `NApiEDEM::IEDEMCoupling::setEDEMTime(double const&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x1ed0): undefined reference to `NApiEDEM::IEDEMCoupling::getEDEMTime(double&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x21e7): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryPosition(int, NApiEDEM::C3dValue&, NApiEDEM::C3x3Matrix&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x221a): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryPosition(int, NApiEDEM::C3dValue&, NApiEDEM::C3x3Matrix&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x2358): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryTranslation(int, NApiEDEM::C3dValue&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x2562): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryVelocity(int, NApiEDEM::C3dValue&, NApiEDEM::C3dValue&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x2710): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryTranslation(int, NApiEDEM::C3dValue&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x291a): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryVelocity(int, NApiEDEM::C3dValue&, NApiEDEM::C3dValue&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x2afd): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryForces(int, NApiEDEM::C3dValue&, NApiEDEM::C3dValue&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x2b6e): undefined reference to `NApiEDEM::IEDEMCoupling::getGeometryForces(int, NApiEDEM::C3dValue&, NApiEDEM::C3dValue&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x2c0a): undefined reference to `NApiEDEM::IEDEMCoupling::removeGeometry(char const*)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x2c28): undefined reference to `NApiEDEM::IEDEMCoupling::showSimulator()'
EPT_Consolidation_Damped_v11.cpp:(.text+0x3b21): undefined reference to `NApiEDEM::IEDEMCoupling::selectGeometrySection(char const*, int&)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x3b39): undefined reference to `NApiEDEM::IEDEMCoupling::removeGeometry(char const*)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x3b48): undefined reference to `NApiEDEM::IEDEMCoupling::showSimulator()'
EPT_Consolidation_Damped_v11.cpp:(.text+0x3fd5): undefined reference to `NApiEDEM::IEDEMCoupling::isConnected() const'
EPT_Consolidation_Damped_v11.cpp:(.text+0x4059): undefined reference to `NApiEDEM::IEDEMCoupling::setGeometryMotion(int, NApiEDEM::C3dValue const&, NApiEDEM::C3x3Matrix const&, NApiEDEM::C3dValue const&, NApiEDEM::C3dValue const&, double, bool)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x40bd): undefined reference to `NApiEDEM::IEDEMCoupling::setGeometryMotion(int, NApiEDEM::C3dValue const&, NApiEDEM::C3x3Matrix const&, NApiEDEM::C3dValue const&, NApiEDEM::C3dValue const&, double, bool)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x40f4): undefined reference to `NApiEDEM::IEDEMCoupling::isConnected() const'
EPT_Consolidation_Damped_v11.cpp:(.text+0x4119): undefined reference to `NApiEDEM::IEDEMCoupling::simulate(double const&, double)'
EPT_Consolidation_Damped_v11.cpp:(.text+0x4dbb): undefined reference to `NApiEDEM::IEDEMCoupling::~IEDEMCoupling()'
EPT_Consolidation_Damped_v11.cpp:(.text+0x4dd1): undefined reference to `NApiEDEM::IEDEMCoupling::~IEDEMCoupling()'
collect2: ld returned 1 exit status
make: *** [bin/Test_v1_5.o] Error 1*

0 个答案:

没有答案