Installable created via CMake on Windows outputting error even when none when debugging it?

huangapple go评论85阅读模式
英文:

Installable created via CMake on Windows outputting error even when none when debugging it?

问题

问题

概要

我目前正在学习CMake教程,以深化我对构建系统的理解。到目前为止,我没有遇到任何问题,但我目前正在第5步 - 练习1上学习如何使用CMake使用install()命令创建可安装的可执行文件。尽管我成功运行了cmake --install。但当我尝试从我的程序x86目录或桌面运行Tutorial.exe文件时,我遇到了如下的运行时问题...

←[31;1mNativeCommandExitException: ←[31;1mProgram "Tutorial.exe" ended with non-zero exit code: -1073741819.←[0m

如果您能提供关于如何修复这个问题或发生了什么的任何见解,我将不胜感激。从我有限的理解来看,MathFunctions.cxxMathFunctions.hmysqrt.cxxmysqrt.h的代码都看起来是正确的,我看不到任何可能导致访问冲突错误的原因。这些函数都很完整,似乎没有处理任何潜在问题的内存操作,比如动态内存分配或数组索引,这些通常是访问冲突的常见原因。

期望

我希望在CLion中测试在build/Debugbuild/Release文件夹中创建的可执行文件时,我的应用程序能够按预期运行。

复现步骤

  1. 下载官方的CMake教程zip文件这里
  2. 转到第5步 - 练习1,如官方CMake教程的这一部分所列(不用担心之前的步骤,它们是独立的)。
  3. 根据练习1中指定的要求编辑Step5/CMakeLists.txtMathFunctions/CMakeLists.txt(参考教程中提供的解决方案)。
  4. 运行cmake --install . --prefix "C:\Users\<YOUR USERNAME>\OneDrive\Desktop\Tutorial\" --config Debug
  5. 注意,当尝试在桌面上的新输出目录的bin文件夹中运行Tutorial.exe时,您会收到运行时错误。但是,当正常运行可执行文件(通过手动输入源文件并使用g++)时,可执行文件中没有运行时错误。

我采取的解决此错误的方法

  • 为了更好地分离问题,我创建了一个名为“cmake-test-installable”的新C++项目,该项目向控制台打印“Hello World”。项目文件结构如下...
    Installable created via CMake on Windows outputting error even when none when debugging it?
  • 以下是该项目的每个相关文件的代码...

main.cpp

#include <iostream>
     
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(cmake_test_installable)

set(CMAKE_CXX_STANDARD 20)

add_executable(cmake_test_installable main.cpp)

# 创建一个可安装的目标
install(TARGETS cmake_test_installable DESTINATION bin)
  • 此外,我使用了与我之前在教程中使用的相同的工具链(MSYS2)和生成器(Ninja)。
    Installable created via CMake on Windows outputting error even when none when debugging it?
  • 同样,我正在使用的CMake的版本版本3.26.4
    Installable created via CMake on Windows outputting error even when none when debugging it?
  • 同样,当我运行cmake --install命令时,它也成功创建了类似教程项目的可执行文件...

Installable created via CMake on Windows outputting error even when none when debugging it?

  • 但是,当我尝试运行现在位于桌面上的可执行文件时,我收到了以下错误...
    Installable created via CMake on Windows outputting error even when none when debugging it?
英文:

Problem

Synopsis

I am currently going through the CMake tutorial to deepen my understanding of the build system. So far, I have had no issues, but I am currently on Step5 - Exercise 1 learning about how to use CMake to make an installable executable using the install() command. While I am successfully running cmake --install. When I attempt to run the Tutorial.exe file either from my program x86 directory or my Desktop, I get a runtime issue as seen below...

←[31;1mNativeCommandExitException: ←[31;1mProgram &quot;Tutorial.exe&quot; ended with non-zero exit code: -1073741819.←[0m

Any insight on how to fix this or what is going on would be appreciated. From my limited understanding it appears the code for MathFunctions.cxx, MathFunctions.h, mysqrt.cxx, and mysqrt.h all look correct and I can't see anything that would cause an access violation error. The functions are well-contained and don't seem to be dealing with any potentially problematic memory operations such as dynamic memory allocation or array indexing, which are the usual suspects in cases of access violations.

Expectation

I am expecting my application to run as expected when I test the executable created in the build/Debug or build/Release folder in CLion.

Steps to Reproduce

  1. Download the Official CMake Tutorial zip file here.
  2. Go to Step5 - Exercise 1 as listed in this part of the official CMake tutorial (Don't worry about the previous steps, they are each independent)
  3. Edit the Step5/CMakeLists.txt and MathFunctions/CMakeLists.txt as specified in Step5 - Exercise 1 (*Refer to the solutions given in the tutorial)
  4. Run cmake --install . --prefix &quot;C:\Users\&lt;YOUR USERNAME&gt;\OneDrive\Desktop\Tutorial\&quot; --config Debug
  5. Notice when trying to run the Tutorial.exe located in the bin folder of your new output directory on your Desktop, you get the runtime error. However, when running the executable normally (either by manually typing in the source files and using g++) the runtime error is not present in the executable.

Approaches I have done to solve this error

  • To better isolate the issue, I have created a new C++ project called "cmake-test-installable" that prints "Hello World" to the console. The project file structure is as follows...
    Installable created via CMake on Windows outputting error even when none when debugging it?
  • Below is the code for each of the relevant files of the project...

main.cpp

#include &lt;iostream&gt;
     
int main() {
    std::cout &lt;&lt; &quot;Hello, World!&quot; &lt;&lt; std::endl;
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(cmake_test_installable)

set(CMAKE_CXX_STANDARD 20)

add_executable(cmake_test_installable main.cpp)

# Create an installable target
install(TARGETS cmake_test_installable DESTINATION bin)
  • Additionally, I am using the same toolchain (MSYS2) and generator (Ninja) as I did previously for the tutorial.
    Installable created via CMake on Windows outputting error even when none when debugging it?
  • Also, the version of CMake I am using is version 3.26.4.
    Installable created via CMake on Windows outputting error even when none when debugging it?
  • Likewise, it also successfully creates an executable similar to the tutorial project when I run the command cmake --install...

Installable created via CMake on Windows outputting error even when none when debugging it?

  • However, when I attempt to run the executable that is now on my Desktop, I get the following error...
    Installable created via CMake on Windows outputting error even when none when debugging it?

答案1

得分: 1

感谢来自@John Hanely的输入,我能够推断出问题是依赖于MinGW(MSYS2)作为工具链和Ninja作为生成器。为什么我不确切知道是什么导致了这个工作,但将我的CMake配置文件更改为依赖于Visual Studio作为工具链和Visual Studio 17 2022作为生成器实际上可以正确生成可安装的可执行文件。

希望这对所有未来的人都有所帮助!

英文:

Thanks to input from @John Hanely I was able to deduce that issue was relying on MinGW (MSYS2) as the toolchain and Ninja as the generator. Why I don't exactly know specifically what causes this to work, changing my CMake profile to rely instead on Visual Studio as the toolchain and Visual Studio 17 2022 as the generator actually correctly generates the installable executable.

Installable created via CMake on Windows outputting error even when none when debugging it?

Hope this helps all future people!

huangapple
  • 本文由 发表于 2023年6月26日 03:29:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定