【问题标题】:C++ Compile Error in LinuxLinux 中的 C++ 编译错误
【发布时间】:2013-11-24 14:18:09
【问题描述】:

以下是我在 Eclipse 上编译良好的代码。 它功能齐全,只是一个打印内存地址的基本程序。

#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
devon@D3V-U91:~/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

谁能告诉我我做错了什么?

【问题讨论】:

  • 没有理由投射你的 malloc 结果。编译器隐式执行它。此外,您应该始终检查 malloc 的返回值是否为负,如果出现错误,请立即退出
  • 你可以看到大多数错误实际上是易于理解和修复的警告(我希望如此)。你只是一个实际的错误对我来说是一个新错误,但这个链接可能会有所帮助,stackoverflow.com/questions/329059/…
  • 你真的应该输入man printf,看看%10lu到底是什么意思。您基本上假设指针的大小与long unsigned int 相同。请改用%p。或者只使用 iostreams。
  • @Stelios:标题和标签说这是 C++(虽然很难相信)。所以演员阵容是必要的。

标签: c++ linux memory compilation g++


【解决方案1】:

文件扩展名是CPP,所以GCC认为是C++文件。但是,您正在尝试与不知道如何链接 c++ 程序的 GCC 链接。使用 g++ 作为命令行,或将文件重命名为 .c 文件。

【讨论】:

  • 你确定吗?所有这些警告都会持续存在,但实际错误是应该修复的链接错误。
  • 链接错误是什么意思?只有一个文件,所以没有其他可以链接到...对吗?
  • @NewFile:错误。您在上面发布的唯一错误是链接器 (ld) 错误。源文件只有一个也没关系。
【解决方案2】:

你想打印内存地址,它们是指针。 printf 格式化标记需要整数。您应该将它们转换为整数,因此将 &pointerVariable 扩展为 (unsigned long int)&pointerVariable。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多