英文:
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.cxx
、MathFunctions.h
、mysqrt.cxx
和mysqrt.h
的代码都看起来是正确的,我看不到任何可能导致访问冲突错误的原因。这些函数都很完整,似乎没有处理任何潜在问题的内存操作,比如动态内存分配或数组索引,这些通常是访问冲突的常见原因。
期望
我希望在CLion中测试在build/Debug
或build/Release
文件夹中创建的可执行文件时,我的应用程序能够按预期运行。
复现步骤
- 下载官方的CMake教程zip文件这里。
- 转到第5步 - 练习1,如官方CMake教程的这一部分所列(不用担心之前的步骤,它们是独立的)。
- 根据练习1中指定的要求编辑
Step5/CMakeLists.txt
和MathFunctions/CMakeLists.txt
(参考教程中提供的解决方案)。 - 运行
cmake --install . --prefix "C:\Users\<YOUR USERNAME>\OneDrive\Desktop\Tutorial\" --config Debug
。 - 注意,当尝试在桌面上的新输出目录的bin文件夹中运行
Tutorial.exe
时,您会收到运行时错误。但是,当正常运行可执行文件(通过手动输入源文件并使用g++)时,可执行文件中没有运行时错误。
我采取的解决此错误的方法
- 为了更好地分离问题,我创建了一个名为“cmake-test-installable”的新C++项目,该项目向控制台打印“Hello World”。项目文件结构如下...
- 以下是该项目的每个相关文件的代码...
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)。
- 同样,我正在使用的CMake的版本是版本3.26.4。
- 同样,当我运行
cmake --install
命令时,它也成功创建了类似教程项目的可执行文件...
英文:
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 "Tutorial.exe" 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
- Download the Official CMake Tutorial zip file here.
- 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)
- Edit the
Step5/CMakeLists.txt
andMathFunctions/CMakeLists.txt
as specified in Step5 - Exercise 1 (*Refer to the solutions given in the tutorial) - Run
cmake --install . --prefix "C:\Users\<YOUR USERNAME>\OneDrive\Desktop\Tutorial\" --config Debug
- 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...
- Below is the code for each of the relevant files of the project...
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)
# Create an installable target
install(TARGETS cmake_test_installable DESTINATION bin)
答案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.
Hope this helps all future people!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论