调用G++;from system()返回一个错误 我编写了一个C++中的简单的TCPSPER,它使用BynFuk代码并将其转换成C++。然后,它应该使用G++和以下代码编译它: std::string compileCommand{ "g++ file.cpp -o file" }; system(compileCommand.c_str());

调用G++;from system()返回一个错误 我编写了一个C++中的简单的TCPSPER,它使用BynFuk代码并将其转换成C++。然后,它应该使用G++和以下代码编译它: std::string compileCommand{ "g++ file.cpp -o file" }; system(compileCommand.c_str());,c++,compiler-errors,g++,mingw,C++,Compiler Errors,G++,Mingw,但当我运行程序时,G++会产生以下错误: c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status 然而,当我跑步的时候 g++ file.cpp -o file …从程序外部,它编译得很好。为什么会发生这种

但当我运行程序时,G++会产生以下错误:

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
然而,当我跑步的时候

g++ file.cpp -o file
…从程序外部,它编译得很好。为什么会发生这种情况?我如何修复它

file.cpp:

#include <iostream>
#include <stdio.h>

int main(int argc, char *argv[])
{
char *ptr = new char[30000]{ 0 };
++*ptr;
++*ptr;
putchar(*ptr)
}
#包括
#包括
int main(int argc,char*argv[])
{
char*ptr=新字符[30000]{0};
++*ptr;
++*ptr;
putchar(*ptr)
}

我无法准确复制您的场景,但我认为库路径可能存在一些问题,正如Michael Doubez在评论中提到的那样。因为这个案例看起来是特定于环境的,所以我认为不会有封闭形式/通用解决方案。如果链接器出现了预期的问题,那么下面的顺序应该可以正常工作-

  • 由于程序在命令行上可以正常编译,因此您应该尝试运行

     g++ file.cpp -Wl,--verbose -o file  
    
    查看默认情况下链接器设置的标志。具体来说,命令输出的
    SEARCH\u DIR
    字段(将有多个字段)将包含链接器将在其中搜索库的目录列表

  • 记下以上步骤中的目录路径,并将这些路径传递给带有“-L”标志的
    compileCommand
    变量示例-

    std::string compileCommand{ "g++ file.cpp -o file -L <path1> -L <path2> -L <path3>" };
    
    库的名称将以“-l”示例--lstdc++、-lgcc_s等开头。记下这些名称,并将其作为标志传递,以及在步骤2中传递的标志。您的
    compileCommand
    变量应如下所示-

    std::string compileCommand{ "g++ file.cpp -o file -L <path1> -L <path2> -L <path3> -lstdc++ -lgcc_s" }; 
    
    std::string compileCommand{“g++file.cpp-o file-L-L-L-lstdc++-lgcc_s”};
    
    如果我们假设这是一个链接器问题是正确的,那么这个序列通常会解决您的问题


  • 请显示一个,包括您的
    文件.cpp
    文件。很可能
    compileCommand
    不是您所希望的。添加诊断消息以验证其值
    std::cout@RSahu我已经尝试了你建议的一切,我在“-o”中添加了空格,compileCommand是正确的-但是G++仍然返回与我上面描述的相同的错误。@Dan,这确实很奇怪。恐怕我没有其他办法来帮助你。祝你好运。错误是链接到mingw库。您的环境不一致。
    
    std::string compileCommand{ "g++ file.cpp -o file -L <path1> -L <path2> -L <path3> -lstdc++ -lgcc_s" };