【问题标题】:main() first defined heremain() 首先在这里定义
【发布时间】:2012-12-25 10:00:57
【问题描述】:

我正在尝试在 Eclipse 中编译我的项目。

但是,它表示main() 被多次定义。我grep'd 我的项目目录,它在 main.cpp 中只找到了一个 main() 的定义。

显然它在其他地方。也许是我链接到的目录。

我链接到的唯一目录是:

-ljson_linux-gcc-4.5.2_libmt

编译器输出为:

make all 
Building file: ../src/main.cpp
Invoking: GCC C++ Compiler
g++ -Ijson_linux-gcc-4.5.2_libmt -I/usr/include/mysql -I/usr/include/jsoncpp-src-0.5.0/include -O0 -g3 -Wall -c -fmessage-length=0 -Ijson_linux-gcc-4.5.2_libmt -MMD -MP -MF"src/main.d" -MT"src/main.d" -o"src/main.o" "../src/main.cpp"
Finished building: ../src/main.cpp

Building target: Atms
Invoking: GCC C++ Linker
g++ -L-L/usr/include/jsoncpp-src-0.5.0/include/ -o"Atms"  ./src/atmstypes.o ./src/base64.o ./src/hregex.o ./src/libparser.o ./src/log.o ./src/main.o ./src/serv.o ./src/sqlfeeder.o ./src/teleindex.o ./src/telepipe.o ./src/telesharedobject.o ./src/treet.o ./src/ttable.o   -l-ljson_linux-gcc-4.5.2_libmt
./src/serv.o: In function `main':
/usr/include/c++/4.4/new:101: multiple definition of `main'
./src/main.o:/home/idan/workspaceCpp/Atms/Debug/../src/main.cpp:12: first defined here
/usr/bin/ld: cannot find -l-ljson_linux-gcc-4.5.2_libmt
collect2: ld returned 1 exit status
make: *** [Atms] Error 1

main.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <iostream>
#include <string.h>
#include <string>
#include "../h/hregex.h"
using namespace std;


string s = "this and7 that";
int main(int argc,char** argv){
    cout << hregex::InitRegex() << endl;
    cout <<  hregex::CheckHostnameField(s)<< "= this and7 that" << endl;
    s = "this and7 that";
    cout <<  hregex::CheckURLField(s)<< "= this and7 that" << endl;
    s = "/lol/idan.html";
    cout <<  hregex::CheckURLField(s)<< "= /lol/idan.html" << endl;
    s = "/lol2#/idan.html";
    cout <<  hregex::CheckURLField(s)<< "= /lol2#/idan.html" << endl;
    return 0;
}

如何防止错误出现?

【问题讨论】:

  • eclipse 会告诉你它在哪里被定义了两次吗?输出控制台中有任何内容吗?
  • 您是否在main.c 中包含任何.c 文件而不是.h 头文件?
  • 我想此时我们需要查看 main.cpp
  • 消息引用/usr/include/c++/4.4/new:101。那里有什么?
  • 出现上述错误的原因是函数的多个定义破坏了One Definition Rule

标签: c++ linux eclipse


【解决方案1】:

g++ 说 serv.o 有一个 main 函数。

如果实际上没有 main() 它 serv.cpp,请检查包含,也许你做了一个错误的 #include 并包含了一个 .cpp 而不是一个 .h ?

作为补充说明: 它尝试绑定库“-ljson_linux-gcc-4.5.2_libmt”

所以链接命令行中有“-l-ljson_linux-gcc-4.5.2_libmt”。删除配置中的 -l

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2021-02-22
    相关资源
    最近更新 更多