#include <stdio.h>
int file1 = 12;
int main()
{
printf("%d\\n",file1);
return 0;
}
#include <stdio.h>
static int file1 = 10;
void file1_func()
{
file1 = file1 + 1;
}
beyes@linux-beyes:~/C/Exam/MultiFile> gcc -g main.c file1.c -o main.exe
beyes@linux-beyes:~/C/Exam/MultiFile> ./main.exe
12
beyes@linux-beyes:~/C/Exam/MultiFile> gcc -g main.c file1.c -o main.exe
/tmp/ccEtJP6E.o:(.data+0x0): multiple definition of `file1'
/tmp/ccGuoh7O.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
beyes@linux-beyes:~/C/Exam/MultiFile> gcc -g main.c file1.c -o main.exe
beyes@linux-beyes:~/C/Exam/MultiFile> ./main.exe
12
beyes@linux-beyes:~/C/Exam/MultiFile> gcc -g main.c file1.c -o main.exe
beyes@linux-beyes:~/C/Exam/MultiFile> ./main.exe
10
beyes@linux-beyes:~/C/Exam/MultiFile> gcc -g main.c file1.c -o main.exe
main.c: In function ‘main’:
main.c:5: error: ‘file1’ undeclared (first use in this function)
main.c:5: error: (Each undeclared identifier is reported only once
main.c:5: error: for each function it appears in.)
#include <stdio.h>
int file1 = 10;
void file1_func()
{
file1 = file1 + 2;
printf("file1_var_file1 = %d\\n", file1);
}
#include <stdio.h>
int file1;
int main()
{
printf("%d\\n",file1);
file1 += 1;
printf("%d\\n",file1);
file1_func();
file1 += 1;
printf("main_var_fiel1: %d\\n", file1);
return 0;
}
beyes@linux-beyes:~/C/Exam/MultiFile> ./main.exe
10
11
file1_var_file1 = 13
main_var_fiel1: 14
Breakpoint 1, main () at main.c:6
6 file1 += 1;
(gdb) p &file1
$1 = (int *) 0x804a014
...
file1_func () at file1.c:7
7 file1 = file1 + 2;
(gdb) s
8 printf("file1_var_file1 = %d\\n", file1);
(gdb) p &file1
$3 = (int *) 0x804a014
static int muluse = 10;
#include <stdio.h>
#include "test.h"
static int muluse;
int main()
{
printf("%d\\n",muluse);
muluse += 2;
printf("%d\\n",muluse);
file2_func();
return 0;
}
#include <stdio.h>
#include "test.h"
static int muluse;
void file2_func()
{
muluse += 1;
printf("muluse = %d\\n",muluse);
}
beyes@linux-beyes:~/C/Exam/MultiFile> gcc -g main.c file2.c -o main.exe
beyes@linux-beyes:~/C/Exam/MultiFile> ./main.exe
10
12
muluse = 11
beyes@linux-beyes:~/C/Exam/MultiFile> gcc -g main.c file2.c -o main.exe
/tmp/ccpEWPw8.o:(.data+0x0): multiple definition of `muluse'
/tmp/ccAO8px8.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
beyes@linux-beyes:~/C/Exam/MultiFile> gcc -g main.c file2.c -o main.exe
file2.c:4: error: static declaration of ‘muluse’ follows non-static declaration
test.h:1: error: previous definition of ‘muluse’ was here
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |