[英]Makefile should search for .o files in another directory
所以我使用 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)
最簡單的方法是在.libs
目錄中創建第二個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
配置文件
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
發出警告消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.