Proper CMake project configuration - c++

I am looking for a guideline regarding handling files that are not source files.
My project is a simple OpenGL application which uses shaders which is built using CMake.
Shaders are in seperate files with .vert .frag extensions respectively and those files are together with others in "src" folder.
Now, these shader files are read when .exe runs I am using Microsoft's C++ compiler, which creates adjacent folder to my src called "build" and in this folder, there is a folder containing my executable called "Debug".
So the structure looks like this:
|
|-src: main.cpp, default_shader.vert, default_shader.frag, etc.
|-build
|- Debug
|- main.exe, [and here I need to have my shaders]
My question is: How do I automate in a neat way moving shader files from my source directory
to this Debug folder?
I was thinking of adding custom target that would run batch file or maybe changing storage of the shaders from files with .vert/frag to something different.

Related

Using cmake to include my source files to another existing project

I'm trying to extend an open source project with my own source files, so that the executable being produced includes my code as well. Probably it's very easy, but somehow I'm overlooking it.
The build process is using cmake and the open source project contains a well written CMakeLists.txt. Instead of adding my source files in the directories of the open source project (and possibly changing CMakeLists.txt of the open source project), I prefer to separate my code in its own directory as follows:
src/opensourceproject/src/...
src/opensourceproject/include/...
...
src/myaddition/include/...
src/myaddition/src/...
Is there a way to wrap the build process and create a CMakeLists.txt that builds opensourceproject with my sources added to the opensource build? The reason why I want it, is that I'm more or less writing a plugin and I want my code to be present in the produced executable.

looking for files in project folder not in the debug/release folder

I'm coding some modern OpenGL which requires external files for the shaders. At this moment, I'm providing the full path of the file inside my application, however, I need to force Qt Creator to look for the files inside the project folder.
As you can see from the perceding image, the files are in the project folder and have been added as DISTFILES in .pro file, however, Qt Creator doesn't search for the files in the project folder. Is there any convenient approach to this problem rather than providing the entire the path as follows.
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "D:\\to the file\\vertexShaderSource.vs");
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, "D:\\to the file\\fragmentShaderSource.fs");

How organize opensource C++ project with IDE to share it on github

I would like to know (according to Principle of Least Surprise) how to organize C++ project with tradeoffs, for example, so that it can be easily shared over git (e.g. on github), or make it easy for anybody who is interested to import the source files into his environment.
As an example I would refer to Code::Blocks since I currently starting to use it, but the question should be more general for any IDE.
How I currently do:
I have directory structure like this ( dictionaries starts with /, comment with // ):
/.git // git repo metadata
/common // source code shared between projects
/math
/physics
/GraphicsUtils
/common_data // shared data (resources)
/apps // executable projects ( use cases )
/SailWar // game about fighting sail boats
/AirCraftSimulator
/SpaceShipSimulator
/libs // dynamic library projects
/OrbitalMechanics
In each project folder for particular executable program I have this structure:
/SailWar
/bin // CodeBlocks binary output
/data // my datafiles (resources) required for program to run
/src // my source code
/obj // CodeBlocks compiled objects files
main.cpp
makefile
program.x // binary
SailWar.cbp // CodeBlocks project file
SailWar.depend // CodeBlocks .depend
SailWar.layout // CodeBlocks .layout
test // bash script to make clean, make and run the binary
In .git/info/exclude I have:
*~ *.pyc *.o *.a *.so *.x bin obj .depend .layout
to make sure that only source files ( not the output and temporary files ) are uploaded to github
What I'm not happy with / sure about:
Should I keep ( track ) .depend .layout in git ?
Is CodeBlocks project .cbp protable and genral enought or I have to still keep the makefile way of compilation. Best would be if CodeBlocks can use makefile instead of .cbp
when compiled binary is put to /bin it does not have correct path to /data
I'm not exactly sure how to best connect shared code /common and resources /common_data. Currently I put ../../common into compiler search paths ... but I'm not sure if it is the best way.
I would probably prefer to make a single project file for everything (all executables, all libraries in one). I think it would make it more clear for any potential user who download it from github. But that is probably not possible (?) (I was reading this question about that.)
I am currently undertaking a pretty big project with many moving parts, so I had to come up with a good way of organizing all my paths so that there was some kind of order to the madness.
Basically, I have a root directory where all my project subfolders reside, and then each project folder has an include folder, but all projects use the same output folder. I also use different .workspace files to organize the different projects into groups. This way I can keep a deterministic folder structure for all my project (all inside the same folder), but still keep them organized.
\root\develop\.workspace
\root\develop\[project]\.cbp
\root\develop\[project]\include
\root\develop\[project]\obj
\root\develop\bin
\root\develop\external\bin
\root\develop\external\include
If you want, you can take a look by checking out the source tree and going through the numerous projects and project options. THis directory structure supports multiple libraries, executables, and external dependencies. Its definitely worth a look.
https://github.com/zackeryfix/ngen/tree/master/develop/now
Making your project depend on IDE is bad idea. It is one of the reasons cmake was created. As such, I'd recommend you to use CMake as your build system and whatever IDE you like, that supports managing CMake projects.
My personal choice is KDevelop, for instance.

OpenGL Configuration Problems. Unresolved external signals

I watch "OpenGL Essentials" to learn OpenGL programming. The author writes a framework himself which contains lots of .hpp and .cpp files. To use the framework, I have to compile all files to a single Library.lib file. But there are two questions that I cannot figure it out.
When use the original sln provided by author, it create two lib files, one in a new created folder "Lib" and one in "Debug" folder, It also copy all hpp files in "Library" folder to a new created "include" folder. But this do not happen when I create a new VC++ project by myself. So how should i configure the VC++ project properties to make that happen.
When we use Glfw, we have to include glfw3.lib, and we also need to tell the exe file where to find the glfw3.dll. That's hard to understand. Because, we can compile our framework to lib file, And we also can compile it to dll file. So, Why we need both lib and dll file for GLfw? What's the difference between this two files.

Linux C++ project directory layout - CMake

I would like to use some standard layout for my linux c++ project which is built using cmake and contains some executables and a library that these execs might link to. Currently I just had a folder for the project and a sub folder for each sub project. With a CMakeLists in the top level and one in each sub level that the opt level adds.
Project-
executable1
executable2
library
However I think it would be better setup like the following
Project -
lib //Library output folder
bin //Executable output folder
src //Al cpp source files
include //All header files
test //All tests
I would have just one CMakeLists in top level.
I can easily set this up in cmake. does anyone have reasons for choosing a different layout?
I wouldn't put the lib, bin and test output directly on the project directory: if you want to make a debug and a release build, you get stuck, because you have only one placeholder. Out of source build is your friend! I would use something like:
Project
src
include
CMakeLists.txt
These will be generated when using cmake:
Project_build_dbg
bin
lib
test
Project_build_release
bin
lib
test
The layout you are proposing to use is pretty much how most of the projects are organised. And its a very well organised way of having a project. I usually also have a docs folder wherein goes all the documentation about the project. So heres my usual project setup.
Project -
lib //Library output folder
bin //Executable output folder
src //All cpp source files
include //All header files
test //All tests
docs //All project documentation ------> new addition