如何解决C 中的头文件问题,未定义的引用
我使用 OpenMPI 用 C 语言编写程序,我使用 Visual Studio 代码作为编辑器。我试图在我的主目录中导入一个链表,但该库是 linekd,因为我没有任何错误,但是里面的函数声明,总是给我 undefiend 引用。 我使用 makefile 并在 vs 代码中使用脚本进行编译,该脚本对于在 mpi 中编译程序很有用。你能帮我吗?
制作文件:
CC = mpicc
all: partitioningtest.o linkedlist.o
$(CC) partitioningtest.c linkedlist.c -o partitioning
头文件:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
struct WordCounter
{
char *word;
int word_count;
struct WordCounter *pNext; /* Pointer to the next word counter in the list */
};
/* Function prototypes */
void addWord(char *pWord); /* Adds a word to the list or updates exisiting word */
void show(struct WordCounter *pWordcounter); /* Outputs a word and its count of occurrences */
struct WordCounter* createWordCounter(char *word);
file.c 没用,因为如果我在主文件中使用它,它可以工作。
无论如何问题是这样的:/tmp/ccQw5qJY.o:在函数main': /root/PcpcGP/partitioningtest.c:77: undefined reference to
addWord'中
/root/PcpcGP/partitioningtest.c:96: 对“show”的未定义引用
collect2:错误:ld 返回 1 个退出状态
struct WordCounter *pCounter = NULL;
struct WordCounter *pStart = NULL;
while ((ch = fgetc(fp)) != EOF){
if(isalnum(ch)!=0 ){
in_word =1;
tmpword[index_of_tmpword] = ch;
index_of_tmpword++;
}/*fine if carattere alfanumerico*/
else{
if ((ch == ' ' || ch == '\t' || ch== '\n') && (in_word==1)){
local_wc++;
word_count++;
in_word=0;
tmpword[index_of_tmpword] ='\0';
index_of_tmpword++;
addWord(tmpword);
memset(tmpword,200);
index_of_tmpword = 0;
}/*fine if tabulazione*/
}/*fine else non è un carattere*/
}
pCounter = pStart;
while(pCounter != NULL){
show(pCounter);
pCounter = pCounter -> pNext;
}
printf("\n \n \n");
代码应该可以工作,因为我在另一个文件中测试了它,当结构在内部声明时。
编辑了linkedlist.c:
#include "linkedlist.h"
struct WordCounter *pStart = NULL;
void show(struct WordCounter *pWordcounter)
{
/* output the word left-justified in a fixed field width followed by the count */
printf("\n%-30s %5d",pWordcounter->word,pWordcounter->word_count);
}
void addWord(char *word)
{
struct WordCounter *pCounter = NULL;
struct WordCounter *pLast = NULL;
if(pStart == NULL)
{
pStart = createWordCounter(word);
return;
}
/* If the word is in the list,increment its count */
pCounter = pStart;
while(pCounter != NULL)
{
if(strcmp(word,pCounter->word) == 0)
{
++pCounter->word_count;
return;
}
pLast = pCounter; /* Save address of last in case we need it */
pCounter = pCounter->pNext; /* Move pointer to next in the list */
}
/* If we get to here it's not in the list - so add it */
pLast->pNext = createWordCounter(word);
}
struct WordCounter* createWordCounter(char *word)
{
struct WordCounter *pCounter = NULL;
pCounter = (struct WordCounter*)malloc(sizeof(struct WordCounter));
pCounter->word = (char*)malloc(strlen(word)+1);
strcpy(pCounter->word,word);
pCounter->word_count = 1;
pCounter->pNext = NULL;
return pCounter;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。