[VS] Visual Studio Working Directory관련

윤태웅·2022년 8월 18일
0

Visual Studio

목록 보기
1/1

개요

DXR프로젝트 진행중 발생한 오류(비주얼 스튜디오 어플리케이션 상에서 디버그 모드로 실행(F5)은 되는데, 아웃풋 디렉토리에 있는 .exe파일은 실행이 불가한 버그)가
발생했고 그 원인과 해결과정을 정리한다.

원인

현재 나는 DXR프로젝트를 다음과 같은 방식으로 진행 중이다.

프로그래밍/CPU디버깅- VS2022
GPU디버깅- Microsoft PIX

다음 코드가 문제가 되었다.

floorMaterial->SetDiffuseTexture(L"Assets\Texture\seafloor.dds");

VS2022에서는 디버그 모드로 실행하면 Working Directory가 Assets폴더를 포함하는 프로젝트 Directory로 설정이 되지만,

위 그림과 Pix에서는 실행한 .exe파일의 Working Directory가 Debug폴더로 설정이 되어서
Assets폴더가 없다고 인식하고 실행이 튕기는 것이었다.

해결

결국 이 버그는 디버그 모드로 실행할 때와 Debug폴더에 있는 .exe파일을 실행할때의 Working Directory가 다른것이 원인이다. 이 두 상황모두 같은 코드가 같은 Directory를 가리키게 하기 위해 다음과 같은 과정을 거쳤다.

  1. 전처리기에 PROJECT_DIR정의
  2. 소스코드에 전처리기 상수-> 문자열화 전처리기 정의
	std::filesystem::path projectDirPath;
	{
		std::string projectDirString = EXPAND(PROJECT_DIR);//project_dir의 문자열화
		projectDirString.erase(0, 1);//Root제거
		projectDirString.erase(projectDirString.size() - 2);// "\."제거
		projectDirPath = projectDirString;
	}
	std::shared_ptr<library::Material> floorMaterial = std::make_shared<library::Material>();//바닥 텍스처
	std::filesystem::path floorTexturePath(L"Assets/Texture/seafloor.dds");//project dir상에서의 상대Path
	floorMaterial->SetDiffuseTexture(std::make_shared<library::Texture>(projectDirPath / floorTexturePath));

어떤 Working Directory환경이어도 Project Directory를 기준으로 Path를 조회하도록 코드 변경

결과

이제 같은 코드로 VS2022에서도 PIX에서도 잘 실행된다.

0개의 댓글