2016-04-03 297 views
0

我試圖從當前文件夾使用um.c 編譯一個名爲um的程序,幷包含一些外部實現。 .h文件位於一個目錄中,但實現這些.h文件的.c文件位於不同的目錄中。以下是我的makefile。看起來編譯器知道在哪裏查找.h文件。但是,鏈接失敗,併產生錯誤:使用makefile從不同目錄鏈接.h文件和.c文件

ld: symbol(s) not found for architecture x86_64 
clang: fatal error: linker command failed with exit code 1 

的Makefile:

# define the C compiler to use 
CC = gcc 

# define any compile-time flags 
CFLAGS = -g -O -Wall -Wextra -Werror -Wfatal-errors -std=c99 -pedantic 

# define any directories containing header files other than /usr/include 
INCLUDES = -I/Users/nguyenmanhduc/Documents/C\ library/cii/include 

# define library paths in addition to /usr/lib 
LFLAGS = -L/Users/nguyenmanhduc/Documents/C\ library/cii/src 

# define any libraries to link into executable: 
LIBS = -lm 

# define the C source files 
SRCS = um.c 

# define the C object files 
OBJS = $(SRCS:.c=.o) 

# define the executable file 
MAIN = um 

.PHONY: depend clean 

all: $(MAIN) 
    @echo Simple compiler named um has been compiled 

$(MAIN): $(OBJS) 
    $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS) 

.c.o: 
    $(CC) $(CFLAGS) $(INCLUDES) -c $< -o [email protected] 

clean: 
    $(RM) *.o *~ $(MAIN) 

depend: $(SRCS) 
    makedepend $(INCLUDES) $^ 

這個問題看似奇怪,因爲我有一個makefile文件沒有什麼經驗,但沒有辦法,我可以鏈接.H的方式和.c文件使用makefile在不同的文件夾中。

謝謝!

回答

1

這裏的問題不是你的額外的.c文件在不同的目錄中:這是你沒有告訴你的Makefile他們存在在所有

在這裏你列出源輸入(我猜你沒有看到有何評論?):

# define the C source files 
SRCS = um.c 

添加其他.c文件,其編譯.o s爲要鏈接的內容。

例如:

# define the C source files 
SRCS = um.c ../wot.c ../hah/lol.c 

沒有硬性規定,但是,你已經構造這個Makefile的方式,那些相對路徑應該能解決就好了。

0

你不鏈接.c文件,你鏈接.o文件。

您似乎在說的是,您的某些.c文件位於不同的目錄中。

無論如何,您必須在makefile中明確列出那些.c文件,就像您使用makefile在目錄中列出.c文件一樣。您必須在另一個目錄中編譯.c文件,然後將它們鏈接起來,就像編譯和鏈接.c文件與makefile所在的目錄一樣。他們不會自行編譯。

另一種方法是在另一個目錄中創建一個單獨的makefile,編譯並構建一個歸檔庫,然後在該目錄中鏈接該歸檔庫。