更改C++程序入口点.STL崩溃了

sla*_*ber 2 c++ gcc stl entry-point

所以我可以成功地将C++入口点更改为一个类,这有助于我将我的图形系统初始化与主程序代码隔离开来.但是当我从新主程序中调用一些库代码时,整个程序崩溃了.例如:

#include <iostream>
using namespace std;
int ENTRY(){
    cout << "hello from here" << endl;
    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

我用这些链接器选项编译它:-e__Z5ENTRYv -nostartfiles
没有/ cout/line它工作正常,否则崩溃与/ Access Violation/at

在此输入图像描述

有什么东西我不见了吗?

Seb*_*edl 5

你到底在想-nostartfiles什么?

它抑制了CRT初始化,除其他外,它负责调用全局构造函数.没有全局构造函数,cout不会初始化.没有初始化,您的程序就会蓬勃发展

为什么要乱用这个呢?在一个小样板中链接是不是更容易main?它可能看起来像这样:

// app.hpp
class App {
protected:
  App();
  virtual ~App();
private:
  virtual int run(/*args if you want them*/) = 0;
  int startup(/*args if you want them*/);
  friend int app_run(/*args if you want them*/);
};

// app.cpp: just add this to your project
namespace { App* the_app; }
App::App() { the_app = this; }
App::~App() {}

int App::startup() {
  // Add whatever code you want here, e.g. to create a window.
  return run();
}

int app_run() { return the_app->startup(); }

int main() { return app_run(); }
int wmain() { return app_run(); }
int WinMain(HINSTANCE, HINSTANCE, char*, int) { return app_run(); }
int wWinMain(HINSTANCE, HINSTANCE, WCHAR*, int) { return app_run(); }
Run Code Online (Sandbox Code Playgroud)

现在只需从App派生您的主类并添加该类型的全局对象.