簡體   English   中英

Makefile 中的 C 鏈接外部庫

[英]C Link External Library in Makefile

編譯時我在鏈接庫(術語框)時遇到問題。 我收到錯誤:

make: *** No rule to make target `termbox.h', needed by `test.o'.  Stop.

生成文件:

edit: test.o
    gcc -Wall -o edit test.o

test.o: test.c termbox/src/termbox.h
    gcc -Wall -c test.c -ltermbox/src

包括:

#include "termbox/src/termbox.h"

我也嘗試過使用編譯庫,但遇到了類似的問題。 我是否必須使用某種指定頭文件和編譯庫位置的組合?

我的termbox文件夾的目錄和test.c在同一個目錄下。

謝謝!

您已經設法編譯並包含庫的頭文件,但是您還沒有告訴編譯器代碼(定義)在哪里——也就是說,您還沒有告訴編譯器鏈接到庫中。

您接下來需要這樣做,這與告訴鏈接器要鏈接哪些文件的方式類似,但有一些額外的語法。 它似乎是一個靜態庫( .a后綴),因此您可以像這樣鏈接:

test.o: test.c termbox/src/termbox.h
    gcc -Wall -c test.c -Itermbox/src -Lsrc -ltermbox

其中-L...指定可以找到庫的位置, -l...指定要鏈接到的庫名稱減去lib前綴和.a.so后綴。 還要注意順序很重要,所以在最后留下庫鏈接。

有關庫鏈接順序的更多信息,請點擊此處

更新

抱歉,我將鏈接添加到了錯誤的行中! - 這是更新的答案:

# The linker stage
edit: test.o
    gcc -Wall -o edit test.o -Lsrc -ltermbox

# Compile stage
test.o: test.c termbox/src/termbox.h
    gcc -Wall -c test.c -ltermbox/src

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM