【问题标题】:Makefile should search for .o files in another directoryMakefile 应该在另一个目录中搜索 .o 文件
【发布时间】:2016-03-21 06:39:08
【问题描述】:

所以我使用 autoconf 来生成 Makefile,并且我已经成功地这样做并编译了我的项目,但问题是目标文件是在 src 文件夹中创建的。我希望它们在.libs 文件夹中编译。

我也可以这样做,但是出现了另一个问题,编译器在与.cpp 文件(即src)相同的文件夹中搜索这些.o 文件。

我已经尝试了所有方法,包括:

%.o: $(SDIR)/%.cpp
        $(CC) $(CFLAGS) -o $(OBJDIR)/$@ -c $<

$(OBJDIR)/%.o: %.c
        $(CC) -c -o $@ $< $(CFLAGS)

还有许多我什至不记得的其他组合。 出于某种原因,这些规则最终不会在 .libs 文件夹中查找目标文件,并且在运行 make 时出现错误:

g++: error: One.o: No such file or directory
g++: error: Two.o: No such file or directory
g++: error: Three.o: No such file or directory
g++: error: Four.o: No such file or directory

这是我的 Makefile.am,到目前为止:

CFLAGS=-Wall -I/chome/siddhs/soft/soft_source_code/target/src/include/ -I/home/siddhs/EclipseProjects/rdma2/include
AM_LDFLAGS=-L /usr/pbs/new/exec/lib/ -lsoft -lpthread -llmx-altair -L /usr/lib/x86_64-linux-gnu/ -lcrypto -L . -lsoft

OUT=rdma2.a
CC=g++
OBJDIR=/home/siddhs/EclipseProjects/rdma2/.libs
SDIR=/home/siddhs/EclipseProjects/rdma2/src
INC=-Iinc
ADIR=/usr/local/rdma2/

bin_PROGRAMS=rdma2test

rdma2test_SOURCES = One.cpp Two.cpp Three.cpp Four.cpp
rdma2test_LDADD=-L /usr/soft/new/exec/lib -lsoft -lpthread -L /usr/lib/x86_64-linux-gnu/ -lcrypto

_OBJS = One.o Two.o Three.o Four.o

%.o: $(SDIR)/%.cpp
        $(CC) $(CFLAGS) -o $(OBJDIR)/$@ -c $<

$(OBJDIR)/%.o: %.c
        $(CC) -c -o $@ $< $(CFLAGS)

【问题讨论】:

  • 您向我们展示了Makefile.am,但是使用它的生成文件在哪里,具有从这些目标文件中实际构建某些内容的规则?

标签: c++ compilation makefile linker autoconf


【解决方案1】:

最简单的方法是在.libs 目录中创建第二个Makefile.am。顶部的Makefile.am 只需指向第二个Makefile.am。第二个将描述所有的构建过程。

一个简单的例子将包含以下内容。

Makefile.am

SUBDIRS = .libs

.libs/Makefile.am

bin_PROGRAMS=rdma2test
noinst_LIBRARIES= librdma2.a
AM_LDFLAGS=-L.
librdma2_a_SOURCES = $(SRC)/One.cpp $(SRC)/Two.cpp
rdma2test_SOURCES = $(SRC)/Main.cpp
rdma2test_LDADD= -lrdma2 

configure.ac

AC_INIT([rdma2],[1.0])
AC_CONFIG_SRCDIR([src/Main.cpp])
AM_INIT_AUTOMAKE
AC_PROG_CXX(g++)
AC_PROG_RANLIB
SRC=`pwd`"/src"
AC_SUBST(SRC)
AC_OUTPUT([Makefile .libs/Makefile])

注意:使用此配置,automake 的更高版本会发出警告消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多