-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
248 lines (220 loc) · 16.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='https://fonts.googleapis.com/css?family=Architects+Daughter' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen" />
<link rel="stylesheet" type="text/css" href="stylesheets/pygment_trac.css" media="screen" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<link rel="icon" type="image/png" href="images/favicon.png">
<meta property="og:title" content="C++ Development in Xcode" />
<meta property="og:type" content="website" />
<meta property="og:url" content="http://jtdaugh.github.io/xcode-umich/" />
<meta property="og:description" content="Tired of Vim? Here's tips for C++ development in Xcode. Tuned to the needs of many classes at UM." />
<meta property="og:image" content="http://jtdaugh.github.io/xcode-umich/images/favicon.png" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-40673113-1', 'github.io');
ga('send', 'pageview');
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<title>C++ Development in Xcode</title>
</head>
<body>
<header>
<div class="inner">
<h1>C++ in Xcode</h1>
<h2>Tired of Vim? Here's tips for C++ development in Xcode. Tuned to the needs of many classes at UM.</h2>
<!-- <a href="https://github.com/jtdaugh/xcode-umich" class="button"><small>View project on</small>GitHub</a> -->
<img class="block-m" src="images/block_m.png"/>
<!-- <div style="clear:both;"/> -->
</div>
</header>
<div id="content-wrapper">
<div class="inner clearfix">
<section id="main-content">
<p></p>
<h1>Contents:</h1>
<ol>
<li><a href="#1">Creating the Project</a></li>
<li><a href="#2">Command Line Arguments</a></li>
<li><a href="#3">Input Redirection - stdin</a></li>
<li><a href="#4">Input and Output files - fstream</a></li>
<li><a href="#5">Debugging</a></li>
<!-- <li><a href="#6">Editing Schemes</a></li> -->
<li><a href="#7">Source Control</a></li>
<li><a href="#8">Testing on CAEN</a></li>
</ol>
<h1 id="1">Creating the Project</h1>
<p style="margin-top:7px;">After opening Xcode, go to File->New->Project</p>
<img src="images/create1.png"/>
<p>In the OS X section, select 'Application' and then select the 'Command Line Tool' option. Then press 'Next'</p>
<img src="images/create2.png">
<p>Name your project and set it's 'Type' to C++</p>
<img src="images/create3.png">
<p>On the next screen select where you your project will be saved and remember to check 'Create local git repository for this project'</p>
<img src="images/create4.png">
<h1 id="2">Command Line Arguments</h1>
<p style="margin-top:7px;">Many projects will require your program to accept several command line arguments.
Let's use an example where we have three arguments, arg1, arg2 and arg3.
If you ran the excecutable from the command line, it would look like this:
</p>
<code>
./project1 arg1 arg2 arg3
</code>
<p>
Sidenote: <code>./project1 arg1 arg2 arg3 < infile.txt</code> Here, infile.txt is NOT a command line argument visible to the program.
It is a redirection to stdin, and is discussed in the <a href="#3">next section</a>.
</p>
<p> Anyway... To add these three arguments in Xcode, go to Project->Edit Scheme</p>
<img src="images/args1.png">
<p>...or click on the project name right next to the Run button.</p>
<img src="images/args2.png">
<p>This window will pop up. If you see something different, make sure you have selected 'Run' on the left, and 'Arguments' on top</p>
<img src="images/args3.png">
<p>Add whatever arguments you need in the 'Arguments Passed on Launch' box, and click Okay. In this case, we add arg1, arg2 and arg3.</p>
<img src="images/args4.png">
<p>Voila! You will now have these three arguments acessible to you as argv[1], 2 and 3.</p>
<h1 id="3">Input Redirection - stdin</h1>
<p style="margin-top:7px;">Let's say your project spec wants you to use a file as input, but using cin (NOT ifstream). Something like this:</p>
<code> ./myExecutable < inputFile.txt </code>
</p>
<p>There is no straightforward way to do this in Xcode, but there is a somewhat simple workaround. First, run your project and make sure it outputs "Hello, World!" to the Xcode Console. Then, to add your input file, right click on your executable name in the 'Products' group in the Xcode Project Navigator and select 'Show In Finder'
</p>
<img src="images/redirect1.png">
<p>Put your input text file in the same directory as your executable (this way Xcode can find it when it is running your program)
</p>
<p>Next go to Project->Edit Scheme and add an environment variable with the value of your text file name
</p>
<img src="images/redirect2.png">
<p>Finally, in your main function (before you do any input processing) add the following:</br>
<pre>
if (getenv("MYARQ")) {
ifstream arq(getenv("MYARQ"));
cin.rdbuf(arq.rdbuf());
}
</pre>
</p>
<p>Now cin will behave the same as if you redirected the contents of a text file to a program, as seen above. If you do not specify the <code>MYARQ</code> environment variable, then it will instead read from cin as normal.</p>
<h1 id="4">Input and Output files - fstream</h1>
<p style="margin-top:7px;">Using ifstream and ofstream is pretty simple in Xcode, and there are (at least) two methods to do so. Here's an example that uses an ifstream. It will copy the contents from file1.txt to file2.txt:</p>
<pre>
ifstream infile("file1.txt");
ofstream outfile("file2.txt");
string temp;
while (!infile.eof()) {
getline(infile,temp);
outfile << temp;
}</pre>
<h4>Option 1</h4>
<p style="margin-top:7px;">The first option is to place the file(s) into the products directory where the excecutable is.
<br>Right click on the products directory and click 'Show in Finder'. Put your input files in there, and find your output files there after running.</p>
<img src="images/redirect1.png" alt="fuckyouandrew.com">
<h4 style="margin-top:15px;">Option 2</h4>
<p style="margin-top:7px;">Another option is to create the input files right within Xcode and then modify 'Copy Files' in the Build Phase. This is a more complicated one-time setup, but will then allow you to edit the input file right within Xcode.
<br>First, create a new empty file in Xcode via File->New->File (or skip this and drag a file into Xcode).</p>
<p>Then, click on the project name right above your list of files. Go to the 'Build Phases' tab, and you'll see this:</p>
<img src="images/fstream3.png">
<p>Add your input file to the 'Copy Files' section. Make sure that 'Products Directory' is the Destination, subpath is empty, and 'Copy only when installing' is unchecked. <small>You may see project1.1 as a copy file. Feel free to remove that.</p>
<img src="images/fstream4.png">
<p>You're all set. Modify the input file in Xcode, and it will copy it into the products directory when you Run. The output file will be in the products directory like in Option 1.</p>
<h1 id="5">Debugging</h1>
<p style="margin-top:7px;">Debugging is pretty awesome in Xcode, and will save you a lot of headache versus using GDB via the command line. To place a breakpoint at a line, click the area immediately to the left of that line of code.</p>
<img src="images/debug1.png">
<p>To enable breakpoints, make sure the 'Breakpoints' button on the top Xcode toolbar is turned on. It will automatically turn on when you set your first breakpoint. In the Navigator on the left, there is also a Breakpoints tab. Here, you can see all of your breakpoints by file, and modify & delete the breakpoints. With this tab selected, the '+' in the bottom left corner of Xcode will allow you to add breakpoints as well. Turning on exception breakpoints can be useful as it occasionally will bring you to the cause of a seg fault.</p>
<img src="images/debug2.png">
<p>When your project hits a breakpoint, you will be presented with the debug window. Here, you have a snapshot of active variables, and the usual options to 'Continue,' 'Step over,' 'Step Into' and 'Step Out.' If you don't see the debug window, you may have the console expanded too much, so click the button right above the right side of the console.</p>
<img src="images/debug3.png">
<!-- <h1 id="6">Editing Schemes</h1>
<p>Yolo ipsum atque ullam but seeing my family have it all harum delectus quo.
Eos minus eveniet odio corporis id ea.
Optio placeat fuckin' right, all right so you cry still,
tears all in the pillow case excepturi optio tempore.
</p>
-->
<h1 id="7">Source Control</h1>
<p style="margin-top:7px;">As long as you checked the 'Create local git repository' back in step 1, if you change or add something in a file, that file should show up modified in Xcode, as seen by the 'M' to the right of the file name
</p>
<img src="images/git1.png"/>
<p>Let's say we want to commit this change, we can do this two ways. The first being through the GUI of Xcode. First, right click on the modified file, and then select Source Control->Commit Selected Files.
</p>
<img src="images/git2.png"/>
<p> Then a screen with the diff of the previous commit and your soon to be new commit will appear, add a commit message at the bottom and then select the 'Commit X Files' button to commit the changes
</p>
<img src="images/git3.png"/>
<p>You can also do this through using the command line: Make a change to a file in your project, it will show up as Modified in Xcode, then on the command line, go to the path of your project. If you type <code> git status</code> you should see that the file you changed is listed as modified.
</p>
<p>To commit the file, simply type <code> git commit -am "My commit message"</code> and this will automatically commit any tracked files (in this case, just our modified file)
</p>
<p>To add files to your git repo, in Xcode, add a new file simply with File->New File. An 'A' should show up next to your new file, showing it has been added since the last commit. To commit through Xcode use the same steps from above: Right click file->Source Control->Commit Selected Files, and then write your commit message and commit.
</p>
<img src="images/git4.png"/>
<p>To add a file to your git repo on the command line, simply type <code> git add mynewfile.h </code> and then when you type <code> git status </code> the newly added file should show up as added. Then simply write, as seen above, <code> git commit -am "My commit message: Commiting my new file"</code> and your newly added file will be commited.
</p>
<h1 id="8">Testing on CAEN</h1>
<p style="margin-top:7px;">Development is awesome in Xcode, but you should be sure to consistently test your code in CAEN, since that is typically the enviroment where your code will be graded. We've used a couple different methods to share code easily between your Mac and CAEN.</p>
<h4>Option 1</h4>
<p style="margin-top:7px;">The first method is setting up a git repo on either <a href="http://ottosipe.github.io/git-notes/" target="_blank">CAEN</a> or Github (or others like Bitbucket), and cloning the same repo on your CAEN. This isn't a git tutorial, so look other places for more help with that. Then you can just push from your mac and pull from CAEN whenever you want to test.</p>
<h4>Option 2</h4>
<p style="margin-top:7px;">The second method is to use scp. In Terminal, cd to your project folder, where you should see project1.xcodeproj and another directory called project1. This inner 'project1' directory should contain your code. To copy all of your code into CAEN and then test, do the following:</p>
<pre>
$ scp -r project1 [email protected]:~/classDir
$ ssh [email protected]
$ cd classDir/project1
</pre>
<p>Then just compile and test your program! If you notice any new bugs or compilation errors, there are some common causes due to the differences between Xcode and CAEN:
<ul>
<li style="margin-top:10px;">Xcode will automatically compile with C++11. If you need C++11, Add the <code>-std=c++0x</code> flag when compiling on CAEN, or check that it is in your Makefile.</li>
<li style="margin-top:10px;">Xcode automatically includes some files. Explicitly <code>#include</code> any libraries or files you need.</li>
<li style="margin-top:10px;">Pseudo-random number generation. If your project ever has you using <code>srand(CONSTANT)</code>, which we've seen in EECS 281, you WILL see a difference between the 'random' numbers generated on OS X and CAEN. If you are seeding based on time do not worry about this.</li>
<li style="margin-top:10px;">Xcode by default uses the Clang compiler. Sometimes you can run into bugs on CAEN that did not exist on OS X if you declare a null pointer like this:</br> <code>MyObject *myPointer;</code></br>If you are initializing a pointer to null in C++11, always do this:</br><code>MyObject *myPointer = nullptr;</code>
</ul>
</p>
<br><br><p>We hope this guide will help you with some of your C++ projects at Michigan. If you have any other tips for us to add, or if you or need clarification on something, feel free to email <a href="mailto:[email protected]">Jesse</a> or <a href="mailto:[email protected]">Chris</a>.
</section>
<aside id="sidebar">
<p class="side-header">Some other great C++ and UMich CS Links:</p>
<p><a class="button" href="http://ryngonzalez.github.io/OptionHandler/" target="_blank">A cleaner solution for handling command line arguments.</a></p>
<p><a class="button" href="http://ottosipe.github.io/git-notes/" target="_blank">Setting up private git repos on CAEN.</a></p>
<p><a class="button" href="http://cplusplus.com" target="_blank">cplusplus.com, a classic</a></p>
<p><a class="button" href="http://shoulditake381.com" target="_blank">Debating whether to take EECS 381?</a></p>
<br/><br/><br/>
<p>Created by <a href="https://github.com/jtdaugh" target="_blank">Jesse Daugherty</a> and <a href="http://github.com/CEWendel" target="_blank">Chris Wendel</a></p>
<p>Last Updated for Xcode 4.5.2</p>
<!-- <p><a class="button" href="#" target="_blank">Download a sample project featuring these tips.</a></p> -->
</aside>
</div>
</div>
<div id="backtotop"> ^ </div>
<script type="text/javascript">
var $root = $('html, body');
$('a').click(function() {
var href = $.attr(this, 'href');
$root.animate({
scrollTop: $(href).offset().top
}, 800, function () {
window.location.hash = href;
});
return false;
});
$(window).scroll(function() {
if($(this).scrollTop() != 0) {
$('#backtotop').fadeIn();
} else {
$('#backtotop').fadeOut();
}
});
$('#backtotop').click(function() {
$('body,html').animate({scrollTop:0},800);
});
</script>
</body>
</html>