【问题标题】:Undefined reference to `main' C++ error in eclipseEclipse 中对“main”C++ 错误的未定义引用
【发布时间】:2026-01-16 03:00:02
【问题描述】:

当我尝试在 main 中进行出色的 oop 时,我有 3 个类的程序我有错误。我尝试用堆栈中的其他帖子更改 gcc 的命令,但错误没有改变。我错过了什么?

我的项目名称是 temp,包含 Region、Sensor 和 Network 类。 我试试这个命令gcc Region_temp.c -o Region_temp

/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22    
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2  
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13 
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2  
make: *** [temp] Error 1
undefined reference to main

main.cpp:

#include <iostream>
#include "Region.h"
using namespace std;
namespace personalization {
    int main() {

        int t ;
        for ( cin >> t ; t != 0; t--){     // reads t different test cases .

            Region region ;

             while(true)
            {
                 int sn , temp;
                 cin >> sn >> temp;
                 if (sn == 0 && temp == 0)
                     break;
                 region.add_sensor(sn , temp) ;
            }
             while (true)
            {
                 int sn1 , sn2 ;
                 cin >> sn1 >> sn2 ;
                 if (sn1 == 0 && sn2 == 0)
                     break;
                 if ( ! ( region . same_network(sn1 , sn2 ) ) )
                     region.join_networks (sn1 , sn2 ) ;
            }
             cout << region . num_of_networks ( ) << " " << region . max_avg_temp ( ) << endl ;
        }

        return 0;
    }
}

【问题讨论】:

  • 能否提供代码示例?
  • @AndreasDM 我将它们附在我的问题中......

标签: c++ linux gcc


【解决方案1】:

您不应该将main 函数放在命名空间中。如果您这样做,编译器(链接器)将找不到它。将其从命名空间中移除。

int main() {

        int t ;
        for ( cin >> t ; t != 0; t--){     // reads t different test cases .

            Region region ;

             while(true)
            {
                 int sn , temp;
                 cin >> sn >> temp;
                 if (sn == 0 && temp == 0)
                     break;
                 region.add_sensor(sn , temp) ;
            }
             while (true)
            {
                 int sn1 , sn2 ;
                 cin >> sn1 >> sn2 ;
                 if (sn1 == 0 && sn2 == 0)
                     break;
                 if ( ! ( region . same_network(sn1 , sn2 ) ) )
                     region.join_networks (sn1 , sn2 ) ;
            }
             cout << region . num_of_networks ( ) << " " << region . max_avg_temp ( ) << endl ;
        }

        return 0;
    }

【讨论】:

  • 您还需要在 main 上方添加“使用命名空间个性化;”
  • 当我不使用namespace std;时,我的错误不会改变@
  • @girl71 ,Chol Nhial 在他的第一条评论中谈到了这个问题。我建议编辑答案以正式添加该注释。
  • @Chol:你也可以把using语句放在 main里面。
【解决方案2】:

gcc Region_temp.c -o Region_temp "-o" 将导致编译器在 Region_temp.c 中查找 main() 并尝试生成名为 Region_temp 的二进制文件

试试gcc -c Region_temp.c,然后是g++ Region_temp.o main.cpp -o Region_temp

当您使用它时,您可能希望将 c++ 文件重命名为 .cpp 并使用 g++ 编译它们

编辑:现在发布了代码示例,@Chol Nhial 已经正确地观察到您还对 main() 进行了命名空间...不要那样做。

【讨论】:

  • 对不起,我在图片中使用了你说的这个命令
  • @girl71 这些是要在终端中使用的命令行命令(不适用于在 eclipse 中运行)。只需在终端中运行它们,无论如何你都会学到更多。
  • tnx ,是的,我是 C++ 和 linux 新手
最近更新 更多