Visual Studio在Release模式下开启debug调试,编译器提示变量已被优化掉,因而不可用
系列文章目录
文章目录
- 系列文章目录
- 前言
- 一、解决办法
- 1.修改工程属性
- 参考
前言
我们在编写代码的时候,如果用到别人的库,而别人只提供了release版本,所有我们也只能生成release版本的工程。但是,我们又想调试代码。如果我们直接调试release版本的代码也是可以的,但经常遇到编译器提示:“变量已被优化掉,因而不可用”,或者提示“未定义标识符”,这样我们在调试代码时,就看不到变量的值,而且还会遇到断点无法命中的情况,感觉好郁闷。
如果没有调整Visual Studio的配置,是无法实现release版本的单步调试功能的。
在Visual Studio一般默认有四种编译方式:
Debug, MinSizeRel, Release, RelWithDebInfo.
RelWithDebInfo模式在保留Release模式下运行快速的前提下,又可以给特定的工程开启Debug模式,进行针对性的调试。这样比整个项目都采用Debug模式进行编译,在调试时会提高效率。
代码示例
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<map>
#include<omp.h>
#include<string>
#include<vector>
#include<algorithm>
#include<sstream>
#include<gdiplus.h>
#pragma comment(lib,"gdiplus.lib")#include <osg/io_utils>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osg/Vec3>
#include <osgViewer/Viewer>
#include <osgGA/TrackBallManipulator>
#include <osg/Texture2D>
#include <osg/Camera>
#include <direct.h>
#include <locale>
#include <codecvt>#include "Ground_Calculate.h"
#include "MeshAnalyzer.h"
#include "myFindNodeVisitor.h"
#include "MyStruct.h"
#include "Shadow_mapping.h"
#include "SolarDirection.h"
#include "Scene_Calculate.h"
#include <math.h>using namespace std;
using namespace Gdiplus;#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )static const double pai = 3.1415926;double radiaToAngle(double radia)
{double temp = radia * pai / 180.0;return temp;
}string TCHAT_to_string(TCHAR* STR)
{int iLen = WideCharToMultiByte(CP_ACP, 0, STR, -1, nullptr, 0, nullptr, nullptr);char* chRtn = new char[iLen * sizeof(char)];WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, nullptr, nullptr);string str(chRtn);int pos = str.find_last_of("\\", str.length());string exePath = str.substr(0, pos);return exePath;
}string getExePath()
{TCHAR szExePath[MAX_PATH];GetModuleFileName(nullptr, szExePath, sizeof(szExePath));string exePath = TCHAT_to_string(szExePath);return exePath;
}string getFilePath()
{char* path = nullptr;path = _getcwd(nullptr, 1);puts(path);string filePath(path);delete path;path = nullptr;return filePath;}//convert string to wstring
std::wstring to_wide_string(const std::string& input)
{std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;return converter.from_bytes(input);
}int main()
{//首先定义文件的输出位置//string result_location = "E:/osg/work/ThermalCharacteristics_20220628/SceneTest/output/";string result_location = getFilePath() + "\\output\\";string resultPos = getFilePath();string exePath = getExePath();//输入气象参数Weather weather1;osgDB::ifstream fin;double data;//fin.open("E:/osg/work/ThermalCharacteristics_20220628/SceneTest/model/20210408xiugai.txt");const char* projPath = getFilePath().c_str();const char* configPath = "\\model\\20210408xiugai.txt";const char* configFullPath = strcat(const_cast<char*>(projPath), configPath);//fin.open(getFilePath().c_str() + "\\model\\20210408xiugai.txt");fin.open(configFullPath);int k = 0;while (1){if (fin.eof()){break;}else{fin >> data;fin >> data;weather1.vec_AirT.push_back(data);fin >> data;weather1.vec_Rh.push_back(data);fin >> data;weather1.vec_WindvSpeed.push_back(data);fin >> data;weather1.vec_Solar.push_back(data);//cout << k << endl;//k += 1;}}weather1.altitude = 0;//地物所在的海拔Weather weather;weather1.vec_AirT.size();for (int i = 0; i < 5; i++) //weather1.vec_AirT.size();{weather.vec_AirT.push_back(weather1.vec_AirT.at(i));weather.vec_Rh.push_back(weather1.vec_Rh.at(i));weather.vec_WindvSpeed.push_back(weather1.vec_WindvSpeed.at(i));weather.vec_Solar.push_back(weather1.vec_Solar.at(i));}weather.altitude = 0;//读取osg地形文件//string osgpath = "E:/osg/work/ThermalCharacteristics_20220628/SceneTest/model/huailai_dem.osg";string osgPath = getFilePath() + "\\model\\huailai_dem.osg";//string osgpath = "E:\\osg\\work\\SceneTest\\model\\FZ.osg";MeshAnalyzer meshanalyzer;meshanalyzer.ReadModel(osgPath, scenechar);
}
一、解决办法
1.修改工程属性
1.C/C+±>优化->优化,修改成:已禁用(/Od)
2. C/C+±>所有选项->调试信息格式:程序数据库(/Zi)
3.连接器->调试->生成调试信息:生成调试信息(/DEBUG)
以上就可以实现release版本的单步调试了,而且几乎和debug版本一样可以看到变量的值了。
参考
梦里寻梦
Visual Studio在Release模式下开启debug调试,编译器提示变量已被优化掉,因而不可用相关推荐
- Visial Studio中“变量已被优化掉 因而不可用”的解决方案
Visial Studio中"变量已被优化掉 因而不可用"的解决方案 参考文章: (1)Visial Studio中"变量已被优化掉 因而不可用"的解决方案 ( ...
- Visial Studio 萌新 错误列表 之 “变量已被优化掉 因而不可用“
废话不多说看图说话依次点击 第五步中选择 已禁用 新装VS是默认进行优化的所以调试时候看不到一些变量 ![在这里插入图片描述](https://img-blog.csdnimg.cn/f017247f ...
- Debug 模式下,出现 变量已被优化掉,因而不可用 的问题
Debug 模式下,出现 变量已被优化掉,因而不可用 的问题. 出现的问题如下: 解决方案如下: 1. 在VS 软件顶部工具栏上,选择 项目 -> 属性,打开属性栏: 2. 打开属性页后,依次点 ...
- Visual Studio Code 编程时如何开启或关闭错误提示
Visual Studio Code 编程时如何开启或关闭错误提示 1.进入文件->首选项->设置 2.在搜索框里输入 C_Cpp.errorSquiggles,将错误提醒设置为打开状态或 ...
- visual studio客户端windows模式下调出cmd命令行
1.visual studio 右击属性 ,如下增加命令行: editbin /SUBSYSTEM:CONSOLE $(outdir)$(ProjectName).exe 显示效果如下:
- vs debug模式下显示“变量已被优化掉,因而不可用”的解决办法
在使用Debug模式对程序进行调试的时候,发现变量的值显示被优化掉了,看不到它的值,如下图所示: 解决办法就是,在vs顶部工具栏上,选择工具->属性,打开属性页,然后依次选择配置属性-& ...
- VS工作笔记-C++在release模式下可以进行调试
原文地址: https://blog.csdn.net/u010565765/article/details/70045196 1,在菜单栏中的生成中,点击配置管理器,在活动解决方案中选择releas ...
- 【Visial Studio疑难杂症】“变量已被优化掉,因而不可用”解决方案
01.目录 文章目录 01.目录 02.问题重现 03.解决方案 04.小结 02.问题重现 有时候,在VS调试中,会出现下面的报错: 导致无法进行正常调试的现象.这是因为我们设置了代码优化,在项目/ ...
- VC++设置Release模式下允许调试代码
2019独角兽企业重金招聘Python工程师标准>>> 在Release中允许代码调试,只需要关闭代码优化,设置调试信息输出就可以了.以VS2015为例,说明如何设置相关项目属性,其 ...
最新文章
- python中编写无参数decorator
- Google Chrome 增加拦截恶意下载的支持
- 傅里叶变换音频可视化_Web Audio在音频可视化中的应用
- unity相机围绕模型转_围绕我们的业务模型和风险进行安全测试
- Opencv之以图像直方图可视化
- 网站日志分析工具:WebLog Expert Lite
- jedis访问redis学习笔记
- Delphi -- 农历算法
- xcode动态改变窗口大小_[SwiftUI 100天] 在 SwiftUI 中动态过滤 @FetchRequest
- Nginx实现静态资源服务器
- uncode-schedule 学习
- 最美春三月,又到一年迎新时——烟台北大青鸟喜迎2019级新生入学报到
- 项目 0: 预测泰坦尼克号乘客生还率
- jerry推荐好玩的做图网站
- c#中winform窗口的隐藏与显示
- 【笔记】29元microbit套装如何玩——套装硬件简介
- springbootjpa之hibernate sql拦截器
- 燕山大学计算机复试有机试吗,关于燕山大学机械考研复试经验及导师信息分享...
- Windows7系统助手
- 段码LCD研究 | 用HT1621B驱动段码LCD屏