c - Undefined reference to global variable during linking -
I am trying to compile a program that is divided into 3 modules, which has 3 source files: ac , bc , and zc . In z.c , the main () function, which calls a function in a.c and b.c . In addition, a function in ac is called a function in b. C , and in the end, a vice-versa is a global variable count And it is defined in a separate header file, global.h .
The code for the source files is as follows:
ac
#include "global.h" #include "bh "#include" ah "int function eb () {function b (); FunctA (); Return 0; } Int functA () {count ++; Printf ("A:% d \ n", counting); Return 0; } BC
#include "global.h" #include "ah" #include "bh" int functBa ( ) {Function A (); FunctB (); Return 0; } Int functB () {count ++; Printf ("B:% d \ n", count); Return 0; } zc
#include "ah" #include "bh" #include "global.h" int main ( ) {Count = 0; FunctAb (); FunctBa (); Return 0; } header files:
ah
#ifndef A_H # defined A_H #include & Lt; Stdio.h & gt; Int function A (); Int functAb (); #endif b.h
#ifndef b_H # Define BHH # Include & lt; Stdio.h & gt; Int functB (); Int functBa (); #endif global.h
#ifndef global_h # global_h extern int count; #endif And, finally, messeal reproduces my error:
cc = gcc cflaj = - O3 -March = Original-Wall -No -United-Results z: Ao Bo Zo Global H $ (CC) -Janz Ao Bo ZO $ (CFLAG) Ao: AC BH Global H $ (CC) -C ACL (CFLAG) BO: BC AH Global H $ (CC) - BC $ (CFLAG): JCH AH Global H $ (CC) -C JSC $ (CFLAGS) With it, I have You can correct the object I have succeeded in reproducing the error in my actual code in this real example, so I think that the dependency between it The problem is the module, but I can not spot it. Can anyone tell me in the right direction? From your to You define the definition in a file by ao , BO , and zo , but, when joining make z , So I get undefined reference to 'count' in all of them:
zo: in function `main ': zc :(. Text.startup + 0x8 ): Undefined reference `count 'ao: in function` functAb': AC: (.text + 0xd): Undefined reference to 'calculation' AC: (. + Text + 0x22): Undefined context for 'count': function `FunctA 'in: AC: (.text + 0x46): Undefined for` counting' AC Reference: (. + Text + 0x5b): Undefined reference to 'count' BO: BC :(. Lesson + 0xd): More undefined re zc to
# Include "ah" #include "bh" #include "global.h" int count; / * Definition Here * / Ent Main () {count = 0; FunctAb (); FunctBa (); Return 0; } global.h , all your files are near announcement variable counts but < Definition is missing in all files. int count = some_value;
Comments
Post a Comment