开发者

How can I pipe stdin from a file to the executable in Xcode 4+?

开发者 https://www.devze.com 2023-04-10 08:15 出处:网络
I have an mpi program and managed to compile and link it via Xcode 4. Now I want to debug it using Xcode 4.

I have an mpi program and managed to compile and link it via Xcode 4. Now I want to debug it using Xcode 4.

How can I pipe the standard input to the program from a file?

In terminal I would type

mpirun -np 2 program < input.txt

I am able to run the program defining a custom executabl开发者_Python百科e (mpirun) in the "Info" panel of the Scheme editor, I also know that I can pass arguments in the "Arguments" panel. But Xcode 4 does not seem to accept "< input.txt" as an argument, even if I check "Use custom working directory" + add the correct directory of the input script in the "Options" panel.

This article Says it is possible to use "< input.txt" as an argument, but I guess that worked in Xcode 2 or Xcode 3, but it does not seem to work in Xcode 4 anymore.


In Xcode 4.5.1:

  1. Open the scheme editor (Product menu -> Edit Scheme...)
  2. Select the Run Debug scheme
  3. In the 'Info' tab panel, change the 'Launch' radio button selection from 'Automatically' to 'Wait for MyApp.app to launch'
  4. Close the scheme editor
  5. Press the Run button to build and run your target. (Xcode's status window in the toolbar will show 'Waiting for MyApp to launch')
  6. Launch Terminal and cd to your built application's folder. (It will be something like /Users/user/Library/Developer/Xcode/DerivedData/MyApp-dmzfrqdevydjuqbexdivolfeujsj/Build/Products/Debug / )
  7. Launch your app piping in whatever you want into standard input:

    echo mydata | ./MyApp.app/Contents/MacOs/MyApp
    
  8. Switch back to Xcode and the debugger will have detected your application's launch and attached to it.


Not wanting to awaken zombies, but this is still the top search result for the question.

I've managed to get this working in XCode 9.2 - but it should be backwards compatible. It uses a subshell and sleep in a pre-action script. This will pipe from a file and stick cout to a file, and supports breakpoints.

(1) Add the following preaction.sh script into your project.

#!/bin/sh
exec > ${PROJECT_DIR}/tests/result.log 2>&1
(
sleep 1
${TARGET_BUILD_DIR}/${EXECUTABLE_NAME} < ${PROJECT_DIR}/tests/file
)&

(2) Then add it to your pre-actions on the Run menu.

How can I pipe stdin from a file to the executable in Xcode 4+?

(3) Ensure your run Waits for executable to be launched.

How can I pipe stdin from a file to the executable in Xcode 4+?

(4) Turn off code signing for your target!

How can I pipe stdin from a file to the executable in Xcode 4+?

Otherwise you will get an error "Message from debugger: unable to attach"

(5) Set breakpoints and run from Xcode as normal

That's all there is to it. The entire trick rests on getting the pre-action script to spawn off a sub-shell that itself delays the process until the run action is launched.


Another approach:

In the very beginning of your program, freopen input file for stdin.

    freopen( "input.txt", "r", stdin );

Conditional macro may help you.

#ifdef DEBUG
    freopen( "input.txt", "r", stdin );
    freopen( "output.txt", "w", stdout );
#endif


Try the following pre-action run script:

mv program program.real
echo -e '#!/bin/sh\nmpirun -np 2 program.real < input.txt' > program

Maybe this will trick XCode into executing your program as you would like it to be executed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号