2016-03-25 14 views
-5

mein erstes Mal wirklich mit #pragma arbeiten und aus irgendeinem Grund bekomme ich nicht die gleiche Ausgabe wie die online gestellt, die Funktionen nicht ausdrucken, ich benutze GCC v5.3 und clang v. 3.7. Hier ist der Code#pragma funktioniert nicht richtig in C?

#include<stdio.h> 

void School(); 
void College() ; 

#pragma startup School 105 
#pragma startup College 
#pragma exit College 
#pragma exit School 105 

void main(){ 
    printf("I am in main\n"); 
} 

void School(){ 
    printf("I am in School\n"); 
} 

void College(){ 
    printf("I am in College\n"); 
} 

und ich kompilieren mit "gcc file.c" und "Klirren file.c". Der Ausgang, den ich bekomme, ist "Ich bin in Haupt"

+3

Wo haben Sie '#pragma startup' und' #pragma exit' in der [GCC Dokumentation] (https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc /Pragmas.html)? –

+2

Pragmas sind Compiler-abhängig, die [GCC Online-Dokumentation über Pragmas] (https://gcc.gnu.org/onlinedocs/gcc/Pragmas.html) führt diese nicht auf, also ist es wahrscheinlich Clang (welches auf GCC abzielt Kompatibilität) hat sie auch nicht. Der [Visual C Compiler hat diese Pragmas auch nicht] (https://msdn.microsoft.com/en-us/library/d9x1s805.aspx). Eine Schnellsuche scheint darauf hinzuweisen, dass sie spezifisch für [Embarcadero C++ Builder] (https://www.embarcadero.com/products/cbuilder) sind. –

+0

http://stackoverflow.com/q/29462376/971127 – BLUEPIXY

Antwort

0

#pragma ist über Compiler nicht konsistent. Es ist nur dazu gedacht, in speziellen Situationen mit bestimmten Compilern/Plattformen verwendet zu werden. Für ein allgemeines Programm wie dieses sollte es nie benutzt werden.

Ein besserer Weg, dies zu erreichen, ist mit #define und #if. Zum Beispiel:

#include<stdio.h> 

#define SCHOOL 1 
#define COLLEGE 2 

#define EDUCATION_LEVEL COLLEGE 

void None(); 
void School(); 
void College(); 

void main(){ 
    #if EDUCATION_LEVEL == SCHOOL 
     School(); 
    #elif EDUCATION_LEVEL == COLLEGE 
     College(); 
    #else 
     None(); 
    #endif 
} 

void None(){ 
    printf("I am in neither school nor college\n"); 
} 

void School(){ 
    printf("I am in School\n"); 
} 

void College(){ 
    printf("I am in College\n"); 
}