简体   繁体   English

将代码链接到exe时C ++程序崩溃,但是将代码编译到exe时工作正常,为什么呢?

[英]C++ program crashes when code is linked to exe but works fine when code is compiled into exe, how come?

I have a program that have been running fine for long time on one platform. 我有一个可以在一个平台上长期运行的程序。 Because of its success it is to be ported to another platform. 由于其成功,它将被移植到另一个平台。 No problem, I thought since it is written in Standard C++... 没问题,我以为是因为它是用标准C ++编写的...

My approach (illustrated with pseudo CMake): 我的方法(用伪CMake进行说明):

  • setup the development environment by sourcing the platform specific toolchain to ensure that correct platform is targeted 通过采购特定于平台的工具链来设置开发环境,以确保针对正确的平台
  • factor out all core business logic into an application object and build a library out of that (one library for each platform from the same source code): 将所有核心业务逻辑分解到一个应用程序对象中,并从该对象中构建一个库(来自相同源代码的每个平台的一个库):

     add_library(appLib STATIC app.cpp) target_link_libraries(appLib utilLib networkLib dbLib ${boostLibs}) 
  • have one main_a.cpp and another main_b.cpp, which do the platform-specific initialization for platform a and b respectively, and let the main function in those instantiate the application object. 有一个main_a.cpp和另一个main_b.cpp,分别对平台a和b进行特定于平台的初始化,然后让main函数实例化应用程序对象。

     int main() { auto result = initAndDoPlatformStuff(); App app(result); app.run(); } 
  • instruct compiler and linker to assemble an executable: 指示编译器和链接器汇编可执行文件:

     if (Platform_A) add_executable(appExe main_a.cpp) else() add_executable(appExe main_b.cpp) endif() target_link_libraries(appExe appLib) 

In principle, this is a perfectly valid approach I guess. 原则上,我猜这是一种完全有效的方法。 But in reality it does not work. 但实际上,它是行不通的。 Within a second program crashes, and the crashes are different almost every time; 在第二个程序崩溃后,崩溃几乎每次都不同。 inspecting the core dumps indicate it sometime crashed in the standard library, sometime in boost library and also in my code, but this is nonsense I guess. 检查核心转储时,它有时会在标准库中崩溃,有时会在boost库中以及我的代码中崩溃,但这是胡说八道。 Program seem to work 1 out 10 times, but eventually crashes. 程序似乎可以工作10次,但最终会崩溃。

However, if I use the same exact code, only extract it into its original main.cpp file and then build it together differently, like this: 但是,如果我使用完全相同的代码,则仅将其提取到其原始main.cpp文件中,然后以不同的方式将其构建在一起,如下所示:

int main()
{
    auto result = initAndDoStuff();
    processForever(result); // Business logic
}
add_executable(appExe main.cpp)
target_link_libraries(appExe utilLib networkLib dbLib ${boostLibs})

then it works! 那就行了!

I'm puzzled and confused. 我感到困惑和困惑。 I'm suspecting it has to do something with code layout, I've therefore played around with different variants of PIC and PIE but have had no success with that. 我怀疑它必须与代码布局有关,因此我尝试了PIC和PIE的不同变体,但没有成功。 Are there any tools available that allows you to get a comprehensive overview of the binary code layout? 有没有可用的工具可让您全面了解二进制代码的布局? I know about nm, od, objdump but they are low-level and I don't know what to look for... Maybe I'm on the wrong path anyway, maybe the problem is related to something completely different. 我知道nm,od,objdump,但是它们是低级的,我不知道要寻找什么...也许我无论如何都走错了路,也许问题与某些完全不同的问题有关。 Does anyone got any hunch of what can cause this behavior? 是否有人会引起这种行为的任何预感? How else can I approach this problem? 我还能如何解决这个问题?

Actually, the fault was mine. 实际上,故障是我的。 Of course... I really tried to get all details correct when I refactored the code into a lib, but obviously I was not careful enough, and blind when searching for the problem. 当然...当我将代码重构为lib时,我确实尽力使所有细节正确无误,但显然我不够谨慎,在寻找问题时视而不见。
The problem, which I finally found, was that I still kept one variable as a local variable after refactoring, which then went out of scope causing deallocated memory to be referenced, which resulted in all sorts of undefined behavior. 我最终发现的问题是,重构后我仍然将一个变量保留为局部变量,然后超出了范围,导致引用了已释放的内存,这导致了各种不确定的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++,代码工作正常,但在提取函数时无法正常工作 - C++, code works fine but not when extracting a function 在 VS Code 中编译 c/c++ 时如何禁用自动创建 exe 文件? - how to disable auto-create exe file when compile c / c++ in VS Code? C ++-当我使用sprintf()函数时程序崩溃(std :: cout正常工作) - C++ - Program crashes when I use sprintf() function (std::cout works fine) VS C ++程序仅在从文件夹运行.exe时有效吗? [不是VS调试] - VS C++ program only works when .exe is run from folder? [not VS debug] 在什么情况下必须编译cpp代码并将其链接到exe中 - Under what conditions must cpp code be compiled and linked into the exe GCC C++“Hello World”程序 -> .exe 在 Windows 上编译时有 500kb 大。 我怎样才能减小它的大小? - GCC C++ “Hello World” program -> .exe is 500kb big when compiled on Windows. How can I reduce its size? 如何让任何用户运行从其C ++代码读取Java方法的.exe程序? - How to let any users run .exe program that reads java methods from its c++ code? 在Visual Studio 2012中单元测试exe中的C ++代码时阻力最小的路径 - Path of least resistance when unit testing C++ code in an exe, in Visual Studio 2012 为什么c++代码运行时会新建一个exe文件是vscode,为什么这么大? - Why does c++ code create a new exe file when it is run isn vscode and why is it so big? 在Eclipse中运行由C ++代码生成的exe文件时,MVCP100D.dll丢失 - MVCP100D.dll missing when running exe file generated by C++ code in Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM