如何链接多个.c文件

时间:2018-12-05 21:18:03

标签: c file c99

我知道您必须创建一个头文件并将其包含在主文件中。我已经做到了,当我出于某种原因编译代码时,无法确定我的函数之一在哪里。

我的项目的布局为:threads.c makeCityFromInput.h和makeCityFromInput.c

我有threads.c#,包括makeCityFromInput.h

这是我尝试编译时发生的情况:

make
gcc -ggdb -std=c99 -Wall -Wextra -pthread  -c threads.c
gcc -ggdb -std=c99 -Wall -Wextra -pthread -o threads threads.o  -lm -lncursesw -pthread
threads.o: In function `main':
/home/project3/threads.c:183: undefined reference to `readConfig'
collect2: error: ld returned 1 exit status
Makefile:71: recipe for target 'threads' failed
make: *** [threads] Error 1

即使位于我的头文件中,也无法找到我的readconfig文件

我的threads.c文件

#define _DEFAULT_SOURCE

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <ncurses.h>  //displays the cars
#include <pthread.h>  //threading
#include <unistd.h>

#include "makeCityFromInput.h"

#define MAX 2048
int main(int argc, char *argv[]){
    //Expected input: Name of config file

    // If the config file is not given; or too many config files are given
    if (argc != 2){
        endwin();
        fprintf(stderr, "Usage: Enter a configuration file");
        exit( EXIT_FAILURE );
    }

    FILE *fp = NULL;
    fp = fopen(argv[1], "r");
    assert( fp );
    readConfig(fp);
    fclose( fp );

    return 0;
}

我的makeCityFromInput.h文件:

#ifndef _MAKECITYFROMINPUT_H
#define _MAKECITYFROMINPUT_H

void readConfig(FILE *fp);

int *parse_line(char *line, int *numInts);

void create_skyline(int *ground, int size, int maxMissiles);



#endif

我的makeCityFromInput.c文件:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <ncurses.h>  //displays the cars
#include <pthread.h>  //threading
#include <unistd.h>
void readConfig(FILE *fp){
    ...
}

我的制作文件

#
# Created by gmakemake (Ubuntu Jul 25 2014) on Wed Nov 14 20:05:05 2018
#

#
# Definitions
#

.SUFFIXES:
.SUFFIXES:  .a .o .c .C .cpp .s .S
.c.o:
        $(COMPILE.c) $<
.C.o:
        $(COMPILE.cc) $<
.cpp.o:
        $(COMPILE.cc) $<
.S.s:
        $(CPP) -o $*.s $<
.s.o:
        $(COMPILE.cc) $<
.c.a:
        $(COMPILE.c) -o $% $<
        $(AR) $(ARFLAGS) $@ $%
        $(RM) $%
.C.a:
        $(COMPILE.cc) -o $% $<
        $(AR) $(ARFLAGS) $@ $%
        $(RM) $%
.cpp.a:
        $(COMPILE.cc) -o $% $<
        $(AR) $(ARFLAGS) $@ $%
        $(RM) $%

CC =        gcc
CXX =       g++

RM = rm -f
AR = ar
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
LINK.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS)
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) -c
COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
CPP = $(CPP) $(CPPFLAGS)
########## Flags from header.mak


CFLAGS =        -ggdb -Wall -Wextra -pedantic -std=c99 -pthread
# program uses pthreads and curses libraries
CLIBFLAGS =     -lncursesw -lpthread -lrt


########## End of flags from header.mak


CPP_FILES = 
C_FILES =   pt-cruisers.c racer.c
PS_FILES =  
S_FILES =   
H_FILES =   racer.h
SOURCEFILES =   $(H_FILES) $(CPP_FILES) $(C_FILES) $(S_FILES)
.PRECIOUS:  $(SOURCEFILES)
OBJFILES =  racer.o 

#
# Main targets
#

all:    pt-cruisers 

pt-cruisers:    pt-cruisers.o $(OBJFILES)
    $(CC) $(CFLAGS) -o pt-cruisers pt-cruisers.o $(OBJFILES) $(CLIBFLAGS)

#
# Dependencies
#

pt-cruisers.o:  racer.h
racer.o:    racer.h

#
# Housekeeping
#

Archive:    archive.tgz

archive.tgz:    $(SOURCEFILES) Makefile
    tar cf - $(SOURCEFILES) Makefile | gzip > archive.tgz

clean:
    -/bin/rm -f $(OBJFILES) pt-cruisers.o core

realclean:        clean
    -/bin/rm -f pt-cruisers 

3 个答案:

答案 0 :(得分:0)

看起来像这样

gcc -ggdb -std=c99 -Wall -Wextra -pthread  -c threads.c
gcc -ggdb -std=c99 -Wall -Wextra -pthread  -c makeCityFromInput.c
gcc -ggdb -std=c99 -Wall -Wextra -pthread -o threads threads.o makeCityFromInput.o -lm -lncursesw -pthread

如果您显示制作文件,我们也可以提供帮助...

答案 1 :(得分:0)

我猜您实际上要问的是“我的makefile有什么问题吗?”。由于您没有提供,我只能进行有根据的猜测。这是构建程序的GNUmake代码段:

.PHONY: default
default: threads

CC      := ...
LD      := ...

CFLAGS  := ...
LDFLAGS := ...
LIBS    := ...

SRCS    := threads.c makeCityFromInput.c
OBJS    := $(SRCS:%.c=%.o)

$(OBJS): %.o: %.c
        $(CC) $(CFLAGS) -o $@ -c $<

threads: $(OBJS)
        $(LD) $(LDFLAGS) -o $@ $^ $(LIBS)

答案 2 :(得分:0)

也许我的想法是错误的,但是我找不到

#include "makeCityFromInput.h"

在您的“ makeCityFromInput.c”中。

“ makeCityFromInput.c”中是否包含“ makeCityFromInput.h”?