2017-02-09 55 views
-2

我試圖從終端編譯兩個c文件時出現問題。 我擁有的文件是:main.c user_info.c。他們都在同一個文件夾中。 當嘗試編譯,我用:GCC的main.c user_info.c -o程序 它給出了一個錯誤消息: 的main.c:3:10:致命錯誤: 'user_info.h' 文件未找到編譯兩個c文件不能正常工作,找不到頭文件

的main.c

#include <stdio.h> 
#include <stdlib.h> 
#include "user_info.h" 

int main() { 

struct user person1; 
struct user person2; 

person1.userId = 1; 
person2.userId = 2; 

puts("Enter the first name of user 1"); 
gets(person1.firstName); 
puts("Enter the first name of user 2"); 
gets(person2.firstName); 

printf("User 1 id is %d\n", person1.userId); 
printf("User 2 first name is %s\n", person2.firstName); 

return 0; 
} 

user_info.c

struct user { 
int userId; 
char firstName[25]; 
char lastName[25]; 
int age; 
float weight; 
}; 
+1

如果實現文件包含'struct'聲明,那麼你的頭文件包含了什麼? – Olaf

+0

如果沒有使用該結構的單獨源文件,則不需要標頭。標題用於在源文件之間共享信息。但是,即使你現在不需要它,你也可以在將來做,或者它是你訓練練習的一部分。 –

回答

3

你user_info.c不user_info.h。如果您正在定義結構,請將user_info.c的名稱更改爲user_info.h,然後嘗試編譯main.c.

+0

謝謝,它的工作。這裏有誤會。 – priit