2013-11-24 6 views
-2

Ниже приведен код, который отлично компилируется на eclipse. Это полностью функциональная, простая программа, которая печатает адреса памяти.Ошибка компиляции C++ в Linux

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

int g0;  /* global variable, uninitialized */ 
int g1 = 14; /* global variable, initialized */ 
int g2[1000]; /* global variable, uninitialized */ 
int g3 = 16; /* global variable, initialized */ 
int g4;  /* global variable, uninitialized */ 

void proc1(); 
void proc2(); 

int main() 
{ 
    extern int etext[], edata[], end[]; 

    int lc0;  /* local variable, stored on stack */ 
    int lc1 = 27; /* local variable, stored on stack; mix init and uninit */ 
    int lc2;  /* local variable, stored on stack */ 
     static int ls0; /* local static variable, uninitialized data */ 
     static int ls1 = 19;  /* local static variable, initialized data */ 
     int *pheap1; 
     int *pheap2; 

     pheap1 = (int *) malloc(sizeof(int)); 
     pheap2 = (int *) malloc(sizeof(int)); 

     printf("\n\n---------- main -------------------\n\n"); 
     printf("%8p (%10lu): Last address\n", 
     0xffffffff, 0xffffffff); 

     printf("%8p (%10lu): Address etext\n", 
     etext, etext); 
     printf("%8p (%10lu): Address edata\n", 
     edata, edata); 
     printf("%8p (%10lu): Address end\n", 
     end, end); 

     printf("%8p (%10lu): Address of code for proc1\n", 
     &proc1, &proc1); 
     printf("%8p (%10lu): Address of code for proc2\n", 
     &proc2, &proc2); 

     printf("%8p (%10lu): Address of uninitialized global variable g0\n", 
     &g0, &g0); 
     printf("%8p (%10lu): Address of initialized global variable g1\n", 
     &g1, &g1); 
     printf("%8p (%10lu): Address of uninitialized global array g2\n", 
     &g2[0], &g2[0]); 
     printf("%8p (%10lu): Address of initialized global variable g3\n", 
     &g3, &g3); 
     printf("%8p (%10lu): Address of uninitialized global variable g4\n", 
     &g4, &g4); 

     printf("%8p (%10lu): Address heap1 in heap space\n", 
     pheap1, (int) pheap1); 
     printf("%8p (%10lu): Address heap2 in heap space\n", 
     pheap2, (int) pheap2); 

    printf("%8p (%10lu): Address of local variable lc0\n", 
     &lc0, &lc0); 
    printf("%8p (%10lu): Address of local variable lc1\n", 
     &lc1, &lc1); 
    printf("%8p (%10lu): Address of local variable lc2\n", 
     &lc2, &lc2); 

    printf("%8p (%10lu): Address of local uninitialized static var ls0\n", 
     &ls0, &ls0); 
    printf("%8p (%10lu): Address of local initialized static var ls1\n", 
     &ls1, &ls1); 

    proc1(); 
    proc2(); 

    return 0; 
} 
void proc1() { 
    int lc3; 
    int lc4 = 37; 

    printf("\n\n----------- proc1 ------------------\n\n"); 
    printf("%8p (%10lu): Address of code for proc1\n", 
     &proc1, &proc1); 
    printf("%8p (%10lu): Address of global variable g0\n", 
     &g0, &g0); 
    printf("%8p (%10lu): Address of global variable g1\n", 
     &g1, &g1); 
    printf("%8p (%10lu): Address of global variable g2\n", 
     &g2[0], &g2[0]); 
    printf("%8p (%10lu): Address of global variable g3\n", 
     &g3, &g3); 
    printf("%8p (%10lu): Address of global variable g4\n", 
     &g4, &g4); 
    printf("%8p (%10lu): Address of local variable lc3\n", 
     &lc3, &lc3); 
    printf("%8p (%10lu): Address of local variable lc4\n", 
     &lc4, &lc4); 
} 

void proc2() { 
    int lc5; 
    int lc6 = 51; 
    static int ls2; 
    static int ls3 = 47; 

    printf("\n\n------------ proc2 -----------------\n\n"); 
    printf("%8p (%10lu): Address of code for proc2\n", 
     &proc2, &proc2); 
    printf("%8p (%10lu): Address of global variable g0\n", 
     &g0, &g0); 
    printf("%8p (%10lu): Address of global variable g1\n", 
     &g1, &g1); 
    printf("%8p (%10lu): Address of global variable g2\n", 
     &g2[0], &g2[0]); 
    printf("%8p (%10lu): Address of global variable g3\n", 
     &g3, &g3); 
    printf("%8p (%10lu): Address of global variable g4\n", 
     &g4, &g4); 
    printf("%8p (%10lu): Address of local variable lc5\n", 
     &lc5, &lc5); 
    printf("%8p (%10lu): Address of local variable lc6\n", 
     &lc6, &lc6); 
    printf("%8p (%10lu): Address of local uninitialized static var ls2\n", 
     &ls2, &ls2); 
    printf("%8p (%10lu): Address of local initialized static var ls3\n", 
     &ls3, &ls3); 
} 

однако, когда я попытался вручную скомпилировать его:

gcc memory_segments.cpp 

я получаю тонны ошибок:

U91:~/Documents/workspace/memory_segments$ gcc memory_segments.cpp 
memory_segments.cpp: In function ‘int main()’: 
memory_segments.cpp:97: warning: format ‘%8p’ expects type ‘void*’, but argument 2 has type ‘unsigned int’ 
memory_segments.cpp:97: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘unsigned int’ 
memory_segments.cpp:100: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:102: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:104: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:107: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’ 
memory_segments.cpp:109: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’ 
memory_segments.cpp:112: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:114: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:116: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:118: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:120: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:123: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int’ 
memory_segments.cpp:125: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int’ 
memory_segments.cpp:128: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:130: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:132: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:135: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:137: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp: In function ‘void proc1()’: 
memory_segments.cpp:150: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’ 
memory_segments.cpp:152: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:154: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:156: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:158: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:160: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:162: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:164: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp: In function ‘void proc2()’: 
memory_segments.cpp:175: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’ 
memory_segments.cpp:177: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:179: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:181: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:183: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:185: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:187: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:189: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:191: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:193: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
/tmp/ccRXp46P.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' 
collect2: ld returned 1 exit status 
[email protected]:~/Documents/workspace/memory_segments$ gcc memory_segments.cpp -lpthread 
memory_segments.cpp: In function ‘int main()’: 
memory_segments.cpp:97: warning: format ‘%8p’ expects type ‘void*’, but argument 2 has type ‘unsigned int’ 
memory_segments.cpp:97: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘unsigned int’ 
memory_segments.cpp:100: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:102: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:104: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:107: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’ 
memory_segments.cpp:109: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’ 
memory_segments.cpp:112: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:114: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:116: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:118: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:120: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:123: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int’ 
memory_segments.cpp:125: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int’ 
memory_segments.cpp:128: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:130: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:132: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:135: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:137: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp: In function ‘void proc1()’: 
memory_segments.cpp:150: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’ 
memory_segments.cpp:152: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:154: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:156: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:158: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:160: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:162: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:164: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp: In function ‘void proc2()’: 
memory_segments.cpp:175: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’ 
memory_segments.cpp:177: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:179: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:181: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:183: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:185: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:187: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:189: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:191: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
memory_segments.cpp:193: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’ 
/tmp/ccJoHGm5.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' 
collect2: ld returned 1 exit status 

Может кто-нибудь сказать мне, что я делаю неправильно?

+1

нет причин бросать результаты вашего malloc. компилятор делает это неявно. Также вы всегда должны проверять свое возвращаемое значение из malloc для отрицательного и немедленно выходить, если есть ошибка. –

+2

Ну, вы можете видеть, что большинство ошибок на самом деле являются предупреждениями, которые легко понятны и легко исправлены (я надеюсь). Вы только фактическая ошибка для меня новая, но эта ссылка может помочь, http://stackoverflow.com/questions/329059/what-is-gxx-personality-v0-for. – john

+2

Вы действительно должны набрать 'man printf' и посмотреть, что именно означает'% 10lu'. Вы в основном предполагаете, что указатель имеет тот же размер, что и 'long unsigned int'. Вместо этого попробуйте '% p'. Или просто используйте iostreams. – kccqzy

ответ

2

Расширение файла CPP, поэтому GCC считает, что это файл C++. Однако вы пытаетесь установить связь с GCC, который не знает, как связать программы на C++. Используйте g ++ для командной строки или переименуйте файл в файл .c.

+0

Я попробовал g ++ file.cpp -lpthread случилась – NewFile

+1

Вы уверены? Все эти предупреждения будут сохраняться, но фактические ошибки - это ошибки ссылок, которые должны быть исправлены. –

+1

Что вы подразумеваете под ссылками ошибок? theres только один файл, поэтому theres ничего другого, котор нужно связать дальше ... правильно? – NewFile

1

Вы хотите печатать адреса памяти, они указатели. Тоны форматирования printf ожидают целые числа. Вы должны отнести их к целым числам, поэтому расширьте & pointerVariable to (unsigned long int) & pointerVariable.

Смежные вопросы