将多个c文件与头文件链接

时间:2015-02-19 21:51:06

标签: c makefile linker

我是新手将c文件链接在一起,并想知道是否有人可以帮我链接带有头文件的2个c文件。在我的make文件中,我有:

all: runMe

runMe: a2.o functions.o

    gcc a2.o functions.o -o runMe

a2.o: a2.c structs.h
    gcc -Wall -pedantic -std=c99 -c a2.c

functions.o: functions.c structs.h
    gcc -Wall -pedantic -std=c99 -c functions.c

其中structs.h具有我使用的结构以及在functions.c中使用的函数原型。我是否将#include“structs.h”放在两个c文件中,或只放入其中一个?如果它不在两者中我得到未声明的标识,未知类型等。如果我将它包括在内,我会收到如下错误:

Undefined symbols for architecture x86_64:
  "_acs_map", referenced from:
  _main in a2.o
  _forward in functions.o
  _backwards in functions.o
"_endwin", referenced from:
  _main in a2.o
"_init_pair", referenced from:
  _forward in functions.o
  _backwards in functions.o
"_initscr", referenced from:
  _main in a2.o
"_printw", referenced from:
  _main in a2.o
"_start_color", referenced from:
  _main in a2.o
"_stdscr", referenced from:
  _main in a2.o
  _forward in functions.o
  _backwards in functions.o
  _restartScreen in functions.o
  _cleanScreen in functions.o
"_waddch", referenced from:
  _main in a2.o
  _forward in functions.o
  _backwards in functions.o
"_wattr_off", referenced from:
  _forward in functions.o
  _backwards in functions.o
"_wattr_on", referenced from:
  _forward in functions.o
  _backwards in functions.o
"_wclear", referenced from:
  _restartScreen in functions.o
  _cleanScreen in functions.o
"_wgetch", referenced from:
  _main in a2.o
"_wgetnstr", referenced from:
  _main in a2.o
"_wmove", referenced from:
  _main in a2.o
  _forward in functions.o
  _backwards in functions.o
"_wrefresh", referenced from:
  _main in a2.o

1 个答案:

答案 0 :(得分:1)

您的标题很好,您可以将它包含在两个C文件中。

错误来自链接器。 它无法找到您在代码中使用的ncurses库函数(initscr,startcolor等)。

您需要首先查找系统中是否安装了ncurses库,如果是,请指定libncurses库可用的路径,如下所示。

runMe: a2.o functions.o
    gcc -LPATH_TO_NCURSES_LIB a2.o functions.o -lncurses -o runMe

你在做什么操作系统? Windows,Linux,BSD?