我必须编写一个具有外部C文件的程序.我最初在Visual Studio中编写了这个程序,但我现在必须切换到gccLinux.我包含了.h允许我在另一个C文件中调用函数的文件.这适用于Visual Studio,但gcc不接受该引用.它在尝试调用函数时中断convertAllStrings(c,size).
错误是: undefined reference to `convertAllStrings'
我在Google上搜索过,发现有些人说这个问题我应该使用这个gcc -I命令.我试过这个,但没有运气.具体我用过:
gcc -I/home/CS/user/unix Main.c -o proj
Run Code Online (Sandbox Code Playgroud)
我有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;
}
Run Code Online (Sandbox Code Playgroud)
文件convertAll.h:
#ifndef convertAll_H_
#define convertAll_H_
void convertAllStrings(char **sentenceList, int numOfSentences);
#endif
Run Code Online (Sandbox Code Playgroud)
文件convertAll.c:
void convertAllStrings(char **sentenceList, int numOfSentences){
printf("function pass 0 is: %s\n", sentenceList[0]);
}
Run Code Online (Sandbox Code Playgroud)
你用过:
gcc -I/home/CS/user/unix Main.c -o proj
Run Code Online (Sandbox Code Playgroud)
你只是在这里编译Main.c.你还没有编译convertAll.c.
你需要:
gcc -I/home/CS/user/unix Main.c convertAll.c -o proj
Run Code Online (Sandbox Code Playgroud)
或者您可以使用以下其中一种:
gcc -I. Main.c convertAll.c -o proj
gcc Main.c convertAll.c -o proj
Run Code Online (Sandbox Code Playgroud)
(顺便说一下,你最好向编译器询问所有警告和调试信息gcc -Wall -Wextra -g)