简体   繁体   中英

Qt qmake and DISTFILES

So I am learning to program in OpenGL 3.3 and I am using qtcreator as my IDE with qmake as the compiler. Everything is fine except I have to read 2 files (fragmentshader.frag and vertexshader.vert) using ifstream. I have included those 2 files in the ".pro" like this:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp \
shaders.cpp

LIBS += -lGLEW -lglfw -lGL -lX11 -lpthread -lXrandr -lXi

DISTFILES += \
vertexshader.vert \
fragmentshader.frag

HEADERS += \
shaders.h

and in the code I try to directly read "vertexshader.vert" and "fragmentshader.frag".

My question is: How do I include these files in my application without having to specify an absolute path?

I had the same problem, it comes from the fact Qt is running files from another directory (by default from the build one).

The Fix

So you have two options:

  • change the Working directory in Projects->Build & Run->Run to %{sourceDir} .
  • change the Build directory in Projects->Build & Run->Build to . or leave it blank.

Explanations

  • The first option run your program (.exe on Windows) from the sourceDir, where your project sits, so path will remain the same relatively.
  • The second option place all your build files (ie: Makefile, *.o and ".exe") in your project directory (they won't appear in the project view unless your Add them), I prefer this way because now your project directory contains everything related to it.

Choose the one you prefer the most, hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM