正确链接库和包含标题

时间:2016-10-09 18:12:41

标签: c makefile linker include static-linking

我在整理我的c程序时遇到了很多麻烦。这个特殊问题似乎源于我编译的链接器阶段。我花了很多时间研究如何正确地做到这一点,现在我已经没有想法了。希望这里有人可以告诉我我错过了什么,或者我把它放在了错误的地方。

这是特殊错误,在发出make all命令后发生:

gcc     readers_writers.c bin/readers_writers   -o readers_writers
readers_writers.c:13:23: fatal error: rtthreads.h: No such file or directory
#include "rtthreads.h"
                   ^
compilation terminated.
<builtin>: recipe for target 'readers_writers' failed

以下是我工作目录中的文件:

monitor.h monitor.c readers_writers.c reader_writer_monitor.c   reader_writer_monitor.h Makefile

这是我需要包含的路径和文件。它是一个实时线程包。

/student/cmpt332/rtt/include/rtthreads.h

这是图书馆的路径:

/student/cmpt332/rtt/lib/Linuxx86_64/libRtt.a

这是我的Makefile的相关内容:

119 RW_C = reader_writer_monitor.c readers_writers.c
120 RW_H = reader_writer_monitor.h
121 RW_OBJ = $(OD)reader_writer_monitor.o $(OD)readers_writers.o
122 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
123   $(CC) $(C_FLAGS) $(RTTIC) -o $@ $(RW_OBJ) $(RTTLC) $(MON_LIB)
124 $(OD)reader_writer_monitor.o : reader_writer_monitor.c $(RW_H)
125   $(CC) $(C_FLAGS) -o $@ -c $< $(MON_LIB)
126 $(OD)readers_writers.o : readers_writers.c $(RW_H)
137   $(CC) $(C_FLAGS) $(RTTIC) -o $@ -c $< $(MON_LIB)

$(BIN),$(OD)是:     ./bin/     ./obj /

以下是所有变量声明:

34 DIR = /student/cmpt332/
35 RTTDIR = $(DIR)rtt/
36 RTTIC = -I$(RTTDIR)include/
37 RTTLC = -L$(RTTDIR)lib/$(CURR_OS)$(ARCH) -m32 -lRtt

从我所学到的(或者我以为我学到的东西),包含路径$(RTTIC)和库路径$(RTTLC)应该是我成功链接库所需的全部内容#39; libRtt.a&#39;我的读者 - 作家计划。

我已经尝试了很多关于这些路径在我的Makefile配方中的位置顺序的小变化,但没有成功。我知道在不同的论坛和地方都有很多线索,但是在我试图解决这个问题的几天之后,我现在已经知道了。任何帮助表示赞赏。

Makefile,来自流行的请求:

 4  # ...
 8  ########################################
 9  # Fancy variable debugging tool
 10 print-%  : ; @echo $* = $($*)
 11 ########################################
 12 # OS and architecture macros
 13 CURR_OS := $(shell uname -s)
 14 ARCH := $(shell uname -m)
 15 MAC_OS = Darwin
 16 LINUX_OS = Linux
 17 WIN_OS = MINGW32_NT-6.1
 18 ########################################
 19 # Select the architecture
 20 ifeq ($(ARCH),x86_64)
 21   BITS=64
 22 else
 23   BITS=32
 24 endif
 25 ########################################
 26 # Check if '-static' can be used?
 27 ifneq ($(CURR_OS), $(MAC_OS))
 28   STATIC = -static
 29 else
 30   STATIC =
 31 endif
 32 #######################################
 33 # THREADS
 34 DIR = /student/cmpt332/
 35 UBCDIR = $(DIR)pthreads/
 36 RTTDIR = $(DIR)rtt/
 37 # RTT THREADS
 38 RTTIC = -I$(RTTDIR)include/
 39 RTTLC = -L$(RTTDIR)lib/$(CURR_OS)$(ARCH) -m32 -lRtt
 40 RTTLCUTILS = -L$(RTTDIR)lib/$(CURR_OS)$(ARCH) -m32 -lRttUtils
 41 # UBC PTHREADS
 42 UBCC = l$(UBCDIR) -m32
 43 UBCL = -L$(UBCDIR)/lib/$(CURR_OS)$(ARCH) -m32 -lpthreads
 44 UBCI = -I$(UBCDIR)
 45
 46 ########################################
 47 BIN = ./bin/
 48 LL = ./lib/$(CURR_OS)$(ARCH)/
 49 OD = ./obj/$(CURR_OS)$(ARCH)/
 50 LIST_LIB = -L$(LL) -llist
 51 MON_LIB = -L$(LL) -lmonitor
 52 ########################################
 53 # Compiler and linker options
 54 CC = gcc
 55 AR_OPTIONS = cr
 56 C_FLAGS = -Wall -pedantic -g -m32
 57 LD_FLAGS = -m32
 58 ########################################
 59 # Recipes
 60 .PHONY: all clean
 61
 62 liblist: $(LL)liblist.a
 63 test_list: $(BIN)my_test_list
 64 libmonitor: $(LL)libmonitor.a
 65 readers_writers: $(BIN)readers_writers
 66 all: builddirs liblist test_list libmonitor readers_writers
 67 ########################################
 68 # build the subdirs for libraries and
 69 # object files
 70 builddirs:
 71     mkdir -p $(LL)
 72     mkdir -p $(OD)
 73     mkdir -p $(BIN)
 74
 75 #=======================================
 76 # LIST LIB
 77 #=======================================
 78 LIST_C = list_adders.c list_movers.c list_removers.c list.c
 79 LIST_H = list.h
 80 LIST_OBJ = $(OD)list_adders.o $(OD)list_movers.o $(OD)list_removers.o $(OD)list.o
 81 # LIST library
 82 $(LL)liblist.a : $(LIST_OBJ)
 83   ar $(AR_OPTIONS) $@ $^
 84 # LIST LIBRARY OBJ FILES
 85 $(OD)list.o : list.c $(LIST_H)
 86   $(CC) $(C_FLAGS) -c $< -o $@
 87 $(OD)list_adders.o : list_adders.c $(LIST_H)
 88   $(CC) $(C_FLAGS) -c $< -o $@
 89 $(OD)list_movers.o : list_movers.c $(LIST_H)
 90   $(CC) $(C_FLAGS) -c $< -o $@
 91 $(OD)list_removers.o : list_removers.c $(LIST_H)
 92   $(CC) $(C_FLAGS) -c $< -o $@
 93 #=======================================
 94 # TEST LIST
 95 #=======================================
 96 LIST_TEST_C = my_test_list.c
 97 LIST_TEST_OBJ = $(OD)my_test_list.o
 98 $(BIN)my_test_list : $(LIST_TEST_OBJ) $(LL)liblist.a
 99   $(CC) $(C_FLAGS) $(LIST_TEST_OBJ) $(LIST_LIB) -o $@
100 $(OD)my_test_list.o : $(LIST_TEST_C) $(LIST_H)
101   $(CC) $(C_FLAGS) -c $< -o $@
102 #=======================================
103 ...
107 #=======================================
108 MON_C = monitor.c
109 MON_H = monitor.h
110 MON_OBJ = $(OD)monitor.o
111 # MONITOR library
112 $(LL)libmonitor.a : $(MON_OBJ)
113   ar $(AR_OPTIONS) $@ $^
114 $(OD)monitor.o : $(MON_C) $(MON_H)
115   $(CC) $(C_FLAGS) -o $@ -c $<
116 #=======================================
117 # READERS WRITERS
118 #=======================================
119 RW_C = reader_writer_monitor.c readers_writers.c
120 RW_H = reader_writer_monitor.h
121 RW_OBJ = $(OD)reader_writer_monitor.o $(OD)readers_writers.o
122 # readers_writers
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124   $(CC) $(C_FLAGS) $(RTTIC) -o $@ $(RW_OBJ) $(RTTLC) $(MON_LIB)
125 # reader_writer_monitor.o
126 $(OD)reader_writer_monitor.o : reader_writer_monitor.c $(RW_H)
127   $(CC) $(C_FLAGS) -o $@ -c $< $(MON_LIB)
128 # readers_writers.o
129 $(OD)readers_writers.o : readers_writers.c $(RW_H)
130   $(CC) $(C_FLAGS) $(RTTIC) -o $@ -c $< $(MON_LIB)

当我没有这个食谱编译时,

122 # readers_writers
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124   $(CC) $(C_FLAGS) $(RTTIC) -o $@ $(RW_OBJ) $(RTTLC) $(MON_LIB)
<。> .o文件readers_writers.oreader_writer_monitor.o编译得很好。当我运行它时,readers_writers配方如下:

22 # readers_writers
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124   $(CC) $(C_FLAGS) -o $@ $(RW_OBJ)

我在两个.o文件中获得了大量未定义的引用,以便在我的$(MON_LIB)和实时线程库中运行。当我使用像这样的食谱编译时:

122 # readers_writers
123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124   $(CC) $(C_FLAGS) -o $@ $(RW_OBJ) $(MON_LIB) $(RTTIC) $(RTTLC) $(MON_LIB)

我收到单一致命错误,告诉我没有这样的文件&#34; rtthreads.h&#34;

我仍然严重亏本。

分辨

好的,终于明白了!在切换我的目录变量($(OD),$(BIN)等)以不包括尾部斜杠,并确保我的-I和-L指令在适当的位置后,我做了几个非常小的更改,让事情正确编译。

这样:

65 readers_writers: $(BIN)readers_writers
66 all: builddirs liblist test_list libmonitor readers_writers

变成了这个:

65 ###
66 all: builddirs liblist test_list libmonitor readers_writers

和指令在这里:

123 $(BIN)readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124   $(CC) $(C_FLAGS) $(RTTIC) -o $@ $(RW_OBJ) $(RTTLC) $(MON_LIB)

变成了这个:

123 readers_writers : $(RW_OBJ) $(LL)libmonitor.a
124   $(CC) $(C_FLAGS) $(RTTIC) -o $@ $(RW_OBJ) $(RTTLC) $(MON_LIB)

这就是全部!那里有一堂课。

0 个答案:

没有答案