Visual Studio在Release模式下开启debug调试,编译器提示变量已被优化掉,因而不可用

系列文章目录

前言

我们在编写代码的时候,如果用到别人的库,而别人只提供了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版本一样可以看到变量的值了。

参考

梦里寻梦

猜你喜欢

转载自blog.csdn.net/aoxuestudy/article/details/125736983