0

I'm coding on CLion and made this log in function. The tests are being made through the .exe because CLion's Terminal sometimes jacks up the I/O's. The problem is my .exe is not finding the files I'm specifying. It runs properly through the CLion terminal, but when shifting to the .exe it doesn't.

I've read that putting those files in the cmake-build-debug/ directory fixes the issue - and it does. Thing is, this is a group project, and by putting those files in that directory I'll constantly run into compatibility troubles when pulling from git - .cmake-build-debug would have to be pushed, thus i'd have to reload it every time. This doesn't seem very proper to me.

The other option is to put the .exe file and required .dll's in the main directory. Again, would have to update this file every single time i build the project, which also isn't a very practical solution.

So I'm asking for some help regarding what can I do to ensure my .exe searches for files in the main directory, not just on the cmake-build-debug directory. The directories are included in CMakeLists, and the .exe still doesn't find them. This is quite the issue. The project will also include some rudimental form of database, so file handling will be important. Would be nice to be able to code and build without having to manually change stuff around every single time.

  • Personally, I would either have it ask for a file location, or hard code in the absolute path to the file and have instructions that your file needs to be in that directory if you want the exe to use it. – NathanOliver Nov 29 '21 at 15:33
  • @NathanOliver I can't hard code the path because it has to run on other people's computers - if there's a way to solve this dynamically please do tell. I included the directories in CMakeLists and pathed inside the working directory – Pedro Barbeira Nov 29 '21 at 15:35
  • In your CMakeLists.txt you can set a compiler definition that contains the path of the folder that contains the data files. With this define you can have your c++ code know absolute path of the files. [https://cmake.org/cmake/help/latest/command/target_compile_definitions.html](https://cmake.org/cmake/help/latest/command/target_compile_definitions.html) – drescherjm Nov 29 '21 at 15:37
  • @drescherjm the data files' folder is in the include_directories directive and the data files are included in the add_executable directive. still doesn't work – Pedro Barbeira Nov 29 '21 at 15:41
  • That should not work. Unless your IDE set the working directory to the same folder as one of the include folders (seems like odd behavior to me) – drescherjm Nov 29 '21 at 15:42
  • @drescherjm wrote that before your edit. checking the link now. thanks – Pedro Barbeira Nov 29 '21 at 15:43
  • There are other options to have CMake variable passed to your c++ code: [https://stackoverflow.com/questions/22259279/passing-a-cmake-variable-to-c-source-code](https://stackoverflow.com/questions/22259279/passing-a-cmake-variable-to-c-source-code) – drescherjm Nov 29 '21 at 15:45
  • @drescherjm ok thanks. this is a bit out of my depth but i'll try and figure it out. thanks for the support – Pedro Barbeira Nov 29 '21 at 15:52
  • Also [https://stackoverflow.com/questions/24488239/use-variable-from-cmake-in-c](https://stackoverflow.com/questions/24488239/use-variable-from-cmake-in-c) – drescherjm Nov 29 '21 at 15:56
  • @drescherjm target_compile_definitions([.exeName] PUBLIC [filePath]) still not working tho. The IDE has the working directory set to the same folder of the include files btw. It runs properly on the IDE terminal, just not on the .exe – Pedro Barbeira Nov 29 '21 at 16:02
  • the .exe builds and runs fine, but is triggering the infile.fail() error message when opening data files (the IDE terminal isn't) – Pedro Barbeira Nov 29 '21 at 16:03
  • It would be more like this `target_compile_definitions(myexe PUBLIC MY_DEFINE="${MY_PATH_TO_THE_DATA}")` where MY_DEFINE would be a c++ definition visible in your code and `MY_PATH_TO_THE_DATA` would be a CMake variable that you defined in your CMakeLists.tx that has the path of the files. – drescherjm Nov 29 '21 at 16:06
  • and then i'd have to call it on the source code. would this work fine with relative paths? – Pedro Barbeira Nov 29 '21 at 16:13
  • One problem with that is if your users use a different IDE the relative path from the working directory of your IDE will be different. For example the default working directory in Visual Studio is the folder containing the project file not the folder of the executable. – drescherjm Nov 29 '21 at 16:31
  • I fix that adding the working directory to the CMakeLists tho, right? in this case the users would run the program through the .exe tho so they wouldn't need to use IDE's – Pedro Barbeira Nov 29 '21 at 16:33
  • CMake does not set the working directory for the IDE however you can do what I said above with the define to get an absolute path to the correct folder regardless of the IDE used. – drescherjm Nov 29 '21 at 16:34
  • @drescherjm i'll give it a go, thanks – Pedro Barbeira Nov 29 '21 at 16:39

0 Answers0