how to change the working directory to the location of the program

后端 未结 4 1629
情书的邮戳
情书的邮戳 2020-12-10 19:55

I want to use c++ to open a file on Mac OS.

If I run the program under Xcode, the working directory is the same with the program, which is fine. However, if I try t

相关标签:
4条回答
  • 2020-12-10 20:10

    You can set a custom working directory for your project in Xcode. In Xcode 4 choose Edit Scheme from the Scheme menu in the project window toolbar to open the scheme editor. Select the Run step from the left side of the project editor. Click the Options button at the top of the scheme editor. Select the Use custom working directory checkbox. Click the button on the right side of the text field to choose the working directory.

    enter image description here

    0 讨论(0)
  • 2020-12-10 20:15

    Use the value $(PROJECT_DIR) in the working directory in your scheme debug settings:

    enter image description here

    0 讨论(0)
  • 2020-12-10 20:28

    You could use chdir(), see here: Change the current working directory in C++.

    Or if you could always just issue a system call (stdlib.h): http://www.cplusplus.com/reference/clibrary/cstdlib/system/. This won't be portable, but it might be good enough for what you need.

    0 讨论(0)
  • 2020-12-10 20:31

    This is a really old post - updating some info for Xcode 12 (Sept 2020)

    Step 1). Xcode -> Product -> Scheme -> Edit Scheme (or create a new one)

    Step 2) RUN(DEBUG), Working Directory(Checkmark) and enter $(PROJECT_DIR) as a starting point.

    0 讨论(0)
提交回复
热议问题