在gcc中包含.h文件时编译C程序

在gcc中包含.h文件时编译C程序,c,gcc,C,Gcc,我必须写一个有外部C文件的程序。我最初是在Visual Studio中编写此程序的,但现在必须在Linux上切换到gcc。我包括了.h文件,该文件允许我调用另一个C文件中的函数。这在VisualStudio中有效,但gcc不接受引用。当尝试调用函数convertAllStrings(c,size)时,它会中断 错误是:未定义对“convertAllStrings”的引用。 我在谷歌上四处搜索,发现一些有这个问题的人说我应该使用gcc-I命令。我试过了,但运气不好。具体而言,我使用了: gcc -

我必须写一个有外部C文件的程序。我最初是在Visual Studio中编写此程序的,但现在必须在Linux上切换到
gcc
。我包括了
.h
文件,该文件允许我调用另一个C文件中的函数。这在VisualStudio中有效,但gcc不接受引用。当尝试调用函数
convertAllStrings(c,size)
时,它会中断

错误是:
未定义对“convertAllStrings”的引用。

我在谷歌上四处搜索,发现一些有这个问题的人说我应该使用
gcc-I
命令。我试过了,但运气不好。具体而言,我使用了:

gcc -I/home/CS/user/unix Main.c -o proj
我在同一个目录中有
Main.C
convertAll.h
convertAll.C
。这是我的密码:

文件Main.c:

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>

#include "convertAll.h"

int main(int ac, char *av[])
{
    int size;
    char strings[100];
    char temp;

    printf("Number of Strings: ");
    scanf("%d", &size);
    char **c = malloc(size);

    int i = 0;
    int j = 0;
    while (i < size)
    {
        printf("Enter string %i ",(i+1));
        scanf("%c", &temp); // temp statement to clear buffer
        fgets(strings, 100, stdin);
        c[i] = malloc(strlen(strings) + 1);
        strcpy(c[i], strings);
        i++;
    }    

    convertAllStrings(c,size); // ** CODE BREAKS HERE

    return 0;
}
文件convertAll.c:

void convertAllStrings(char **sentenceList, int numOfSentences){

    printf("function pass 0 is: %s\n", sentenceList[0]);

}
您使用了:

gcc -I/home/CS/user/unix Main.c -o proj
您在这里只编译Main.c。您还没有编译convertAll.c

你需要:

gcc -I/home/CS/user/unix Main.c convertAll.c -o proj
或者你也可以使用其中一种:

gcc -I. Main.c convertAll.c -o proj
gcc     Main.c convertAll.c -o proj

(顺便说一句,您最好使用gcc-Wall-Wextra-g查看所有警告和调试信息)

这不起作用:

gcc -I/home/CS/user/unix Main.c -o proj
试图编译
main.c
并将其链接到
proj

也就是说,它甚至不使用convertAll.c文件

建议如下:

gcc -c -Wall -Wextra -Wconversion -pedantic -std=gnu11 Main.c -o Main.o  -I.
gcc -c -Wall -Wextra -Wconversion -pedantic -std-gnu11 convertAll.c -o convertAll.o -I.
gcc Main.o convertAll.o -o proj
VisualStudio为您这样做,因为您将文件包括在项目中

您可以编写一个
Makefile
来完成所有这些,然后使用

make
然后用

./proj
以下是适合您的项目的Makefile

SHELL  := /bin/sh

NAME   := proj


#
# macro of all *.c files 
# (NOTE:
# (the following 'wildcard' will pick up ALL .c files
# (like FileHeader.c and FunctionHeader.c 
# (which should not be part of the build
# (so be sure no unwanted .c files in directory
# (or change the extension
#
SRC := $(wildcard *.c)
OBJ := $(SRC:.c=.o)
DEP := $(SRC:.c=.d)
INC := $(SRC:.c=.h)  # or perhaps INC := convertAll.h

MAKE    :=  /usr/bin/make

CC      :=  /usr/bin/gcc

CP      :=  cp

LDFLAGS := 

DEBUG   :=  -ggdb3

CFLAGS :=  $(DEBUG) -Wall -Wextra -pedantic -Wconversion -std=gnu11

LIBS    :=   

.PHONY: all
all : $(NAME)


#
# link the .o files into the executable 
# using the linker flags
# -- explicit rule
#
$(name): $(OBJ)
    #
    # ======= $(name) Link Start =========
    $(CC) $(LDFLAGS) -o $@ $(OBJ) $(COMMON_OBJ) $(LIBS)
    # ======= $(name) Link Done ==========
    #


#
#create dependancy files -- inference rule
#
%.d: %.c 
    # 
    # ========= START $< TO $@ =========
    $(CC) -M $(CPPFLAGS) $< > $@.$$$$;                      \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;     \
    rm -f $@.$$$$
    # ========= END $< TO $@ =========


# 
# compile the .c files into .o files using the compiler flags
# -- inference rule
#
%.o: %.c %.d 
    # 
    # ========= START $< TO $@ =========
    $(CC) $(CFLAGS) -c $< -o $@ -I. 
    # ========= END $< TO $@ =========
    # 


.PHONY: clean
clean: 
    # ========== CLEANING UP ==========
    rm -f *.o
    rm -f $(name).map
    rm -f $(name)
    rm -f *.d
    # ========== DONE ==========


# include the contents of all the .d files
# note: the .d files contain:
# <filename>.o:<filename>.c plus all the dependancies for that .c file 
# I.E. the #include'd header files
# wrap with ifneg... so will not rebuild *.d files when goal is 'clean'
#
ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEP)
endif
SHELL:=/bin/sh
名称:=项目
#
#所有*.c文件的宏
#(注:
#(以下“通配符”将拾取所有.c文件
#(如FileHeader.c和FunctionHeader.c
#(不应作为构建的一部分)
#(所以请确保目录中没有不需要的.c文件。)
#(或更改扩展名
#
SRC:=$(通配符*.c)
对象:=$(SRC:.c=.o)
DEP:=$(SRC:.c=.d)
INC:=$(SRC:.c=.h)#或者INC:=convertAll.h
MAKE:=/usr/bin/MAKE
抄送:=/usr/bin/gcc
CP:=CP
LDFLAGS:=
调试:=-ggdb3
CFLAGS:=$(调试)-Wall-Wextra-pedantic-Wconversion-std=gnu11
LIBS:=
冒牌货:全部
全部:$(名称)
#
#将.o文件链接到可执行文件中
#使用链接器标志
#--明确规则
#
$(名称):$(OBJ)
#
#=======$(名称)链接开始=========
$(CC)$(LDFLAGS)-o$@$(OBJ)$(COMMON_OBJ)$(LIBS)
#=======$(名称)链接完成==========
#
#
#创建依赖性文件--推理规则
#
%d.d:%
# 
#=========开始$<到$@=========
$(CC)-M$(CPPFLAGS)$<>$@.$$$\
sed's,\($*\)\.o[:]*,\1.o$@:,g'<$@.$$>$@\
rm-f$@$$$$
#=======结束$<到$@=========
# 
#使用编译器标志将.c文件编译成.o文件
#--推理规则
#
%.o:%.c%.d
# 
#=========开始$<到$@=========
$(CC)$(CFLAGS)-c$<-o$@-I。
#=======结束$<到$@=========
# 
.假冒:干净
清洁:
#===============清理==========
rm-f*.o
rm-f$(名称).map
rm-f$(名称)
rm-f*.d
#=============完成==========
#包括所有.d文件的内容
#注意:.d文件包含:
#.o:.c加上该.c文件的所有依赖项
#即#include'd头文件
#使用ifneg包装…因此在目标为“干净”时不会重建*.d文件
#
ifneq“$(MAKECMDGOALS)”“干净”
-包括美元(DEP)
恩迪夫

注意:缩进的行必须由
char

进行这似乎是一个链接错误,而不是编译错误。您是否链接了两个目标文件以创建可执行文件?您应该
#包括“convertAll.h”
convertAll.c
中,这样您就可以从交叉检查中获益。当然,您还需要
包括
char**c=malloc(size);
这将太小。您需要
char**c=malloc(size*sizeof*c)
try:gcc-I/home/CS/user/unix Main.c convertAll.o-o projLearn使用一些生成器自动化工具,如GNU
make
@melpomene,虽然它“可能”是多余的,但它确实告诉
make
在哪里可以找到“自产”头文件。我认为这是一个“良好的实践”,因此在处理更复杂的项目时不会忘记它它告诉
make
nothing.
make
不会查看构建规则内部。
SHELL  := /bin/sh

NAME   := proj


#
# macro of all *.c files 
# (NOTE:
# (the following 'wildcard' will pick up ALL .c files
# (like FileHeader.c and FunctionHeader.c 
# (which should not be part of the build
# (so be sure no unwanted .c files in directory
# (or change the extension
#
SRC := $(wildcard *.c)
OBJ := $(SRC:.c=.o)
DEP := $(SRC:.c=.d)
INC := $(SRC:.c=.h)  # or perhaps INC := convertAll.h

MAKE    :=  /usr/bin/make

CC      :=  /usr/bin/gcc

CP      :=  cp

LDFLAGS := 

DEBUG   :=  -ggdb3

CFLAGS :=  $(DEBUG) -Wall -Wextra -pedantic -Wconversion -std=gnu11

LIBS    :=   

.PHONY: all
all : $(NAME)


#
# link the .o files into the executable 
# using the linker flags
# -- explicit rule
#
$(name): $(OBJ)
    #
    # ======= $(name) Link Start =========
    $(CC) $(LDFLAGS) -o $@ $(OBJ) $(COMMON_OBJ) $(LIBS)
    # ======= $(name) Link Done ==========
    #


#
#create dependancy files -- inference rule
#
%.d: %.c 
    # 
    # ========= START $< TO $@ =========
    $(CC) -M $(CPPFLAGS) $< > $@.$$$$;                      \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;     \
    rm -f $@.$$$$
    # ========= END $< TO $@ =========


# 
# compile the .c files into .o files using the compiler flags
# -- inference rule
#
%.o: %.c %.d 
    # 
    # ========= START $< TO $@ =========
    $(CC) $(CFLAGS) -c $< -o $@ -I. 
    # ========= END $< TO $@ =========
    # 


.PHONY: clean
clean: 
    # ========== CLEANING UP ==========
    rm -f *.o
    rm -f $(name).map
    rm -f $(name)
    rm -f *.d
    # ========== DONE ==========


# include the contents of all the .d files
# note: the .d files contain:
# <filename>.o:<filename>.c plus all the dependancies for that .c file 
# I.E. the #include'd header files
# wrap with ifneg... so will not rebuild *.d files when goal is 'clean'
#
ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEP)
endif