Domanda

I have second level include that is giving me grief:

Undefined                       first referenced
 symbol                             in file
function2                      /var/tmp//ccAPaWbT.o
ld: fatal: symbol referencing errors. No output written to run
collect2: ld returned 1 exit status

Main file:

#include "functions02.c"
int main(){
int x = funcion2();
}

functions02.c file:

#ifndef FUNCTIONS02_C
#define FUNCTIONS02_C
int funcion2();
#if __INCLUDE_LEVEL__ == 0
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "functions01.c"
int main() {
   return function2();
}
#endif
int function2()
   return function1();
}
#endif

functions01.c file:

#ifndef FUNCTIONS01_C
#define FUNCTIONS01_C
int funcion1();
#if __INCLUDE_LEVEL__ == 0
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main() {
   return function1();
}
#endif
int function1()
   return 10;
}
#endif

I am assuming that this can be fixed manipulated using __INCLUDE_LEVEL__ or manipulating linking on gcc compiling but I can't find forking variant.

First of all is it possible to achieve what I am looking for without putting functions in external header files? Second what would be the correct way of doing it?

EDIT: I realized that I had forgotten to add function dependencies to them. That is the includes which are used by functions can not be excluded by adding them just next to main function warped in exclusion if close.

È stato utile?

Soluzione

Yes, it is possible to include / exclude any part of the code using compile time flags (and in the code using #if's) as you are trying to do.

In your case, I assume you have not defined __INCLUDE_LEVEL__ flag, hence the linker is not able to find funciton2, so the error.

If you define it, you will have three "main()" :-), it will fail again. So, you need to rework your code a bit.

Also, #include'ing a "C" file is not advisable and not used in practice. I assume you are just trying to experiment & learn, which is fine.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top