reading files from sourceDir rather than sourceDir/build/project.app/Contents/MacOS/

Hi everyone,

I’m currently working on the example from [https://examples.vtk.org/site/Cxx/IO/ReadSTL/]. I noticed that the STL files are only successfully read when I place them in the directory: /source/cmake-build-debug/VTK_importSTL.app/Contents/MacOS/.

I would like to read the files directly from the /source directory instead.
I have already add include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ) in CMakeLists.txt and add $ContentRoot$ to the Working Directory but neither of these works.
Could this be solved by changing my CMake configuration?

Here is my CmakeLists.txt

cmake_minimum_required( VERSION 3.29 )
project( VTK_importSTL )
find_package( VTK COMPONENTS
              CommonColor
              CommonCore
              IOGeometry
              InteractionStyle
              RenderingContextOpenGL2
              RenderingCore
              RenderingFreeType
              RenderingGL2PSOpenGL2
              RenderingOpenGL2
)
if(NOT VTK_FOUND)
    message( FATAL_ERROR "VTK components: Unable to find the VTK build folder." )
endif()

# Prevent a "command line is too long" failure in Windows.
set( CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files." )
add_executable( VTK_importSTL MACOSX_BUNDLE main.cpp )
target_link_libraries( VTK_importSTL PRIVATE ${VTK_LIBRARIES} )

include_directories( ${CMAKE_CURRENT_SOURCE_DIR} )

# vtk_module_autoinit is needed
vtk_module_autoinit(
        TARGETS VTK_importSTL
        MODULES ${VTK_LIBRARIES}
)

I would greatly appreciate any guidance, thanks in advance

If I understood correctly, you want to read .stl files at runtime from a specific folder?

include_directories has nothing to do with where files are searched for at runtime, it only affects where the compiler searches for include files during compile time.

You either need to specify an absolute path to the .stl (something like /source/test.stl), or specify a path relative to the current working directory; the working directory in your case seems to be /source/cmake-build-debug/VTK_importSTL.app/Contents/MacOS/, so a relative path to a file test.stl in /source would be ../../../../test.stl.

Alternatively, you could change the working directory for your application; there are however many ways to run your program (via IDE, via shell, etc…), and this influences how you specify the working directory, so you’d have to tell us how you are actually running the application. I suspect you are running it directly via XCode?

You could set the working folder manually as described here - probably a bit outdated, but it should give an idea where to look.

You should also be able to set the XCode working directory via CMake.

I have already added include_directories(${CMAKE_CURRENT_SOURCE_DIR}) in CMakeLists.txt and included $ContentRoot$ in the Working Directory, but neither of these worked.

Okie Dokie, I just tested adding $ContentRoot$(namely sourceFileDir) to the Working Directory in my CLion 2024.2.2 on a Windows system. It worked pretty well for reading the files from the other directory.

I can speculate that this is an annoying bug due to some configuration or setting conflicts between CLion 2024.2.2 and macOS. I’ll report it to JetBrains.
In any case, thank you for your help. Next time, I won’t suspect it’s an issue with my CMake, at least.

Oh I had missed that one in the original post! How did you set the working directory, via IDE or via CMake? The CLion part definitely clears things up for me as well.

In any case, thank you for your help. Next time, I won’t suspect it’s an issue with my CMake, at least.

Not sure if my answer was actually helping or more confusing :wink: - There are ways to set the working directory via CMake, but they are IDE-specific; not sure if there is one for CLion!