Trouble running Xcode executable
I am having an issue with Xcode and the executable that is produced. I am working in a C command line tool project. When I build and run the code the code runs fine from the debug command line; however, if I double click the executable it asks me for the file name I want to open but then I get a segmentation fault error. I then took the .c file and compiled it with gcc and it runs fine if I run it using ./ ; however, if I double click the file it asks me for the file name I want to open but then I get the segmentation fault error again. I am not overly experienced with C (which may be apparent from this post) but am learning it for a HPC class I am taking next semester. Thanks in advance.
Jeff




Just a wild guess ...
What *exactly* does it prompt you for when you click on the executable (in the Finder, I presume)? When I do this on my machine, it runs the executable in the Terminal. But maybe Unix executables are not associated with Terminal.app on your machine, and Finder is prompting you for that?
When I click the executable
When I click the executable Terminal opens a window and see the following
178:~ username$ /Users/username/Desktop/readtest ; exit;
Input filename (must be less than 50 characters including the extension:
The "Input filename...." is from a printf statement in my program. What should happen next is I type a name (A.dat for example) and the program will open up the file (which is a matrix) and display the row number, column number, and then list every element of the matrix. This is what happens if I run the program using ./readtest or if I use Build and Run from Xcode and enter the filename through the debugger. If I double click the executable and enter the file name the result is this:
Input filename (must be less than 50 characters including the extension: C.dat
Opening file C.dat
Segmentation fault
logout
[Process completed]
The "Opening file...." is also from a printf statement. Thanks for the help.
Jeff
Attach debugger?
Ah - that means your program does run when you double-click it, but it crashes. I don't think this has anything to do with Xcode, it's just a bug in your program. You could attach a debugger to it when it asks for the file name, or you could locate the statement causing the segfault using simple print statements (I assume the code is quite short). What are you using to read the matrix? If it is scanf, I'd bet on this :)
Reading with fscanf
I am not that familiar with using the debugger other than reading the error messages. Where is a good source I could read up on that? I'm sure I can find a fair number of sources floating around the internet.
I use fscanf to read in the matrix. The matrix files have the following format:
First line consists of two integers separated by a tab. The first int is the row number the second is the column number
Every other line corresponds to the rows of the matrix. Elements are separated by tabs.
I don't expect you to go through my code and debug it for me but in case it helps here is the code:
#include
FILE *f1;
int main (int argc, const char * argv[]) {
char inputname[50];
int matnum1, matele, m, n; /* Define ints */
printf("Input filename (must be less than 50 characters including the extension: "); /* Get file name */
scanf("%s", inputname);
printf("Opening file %s\n",inputname); /* Print filename to command line for verification */
f1 = fopen(inputname, "r"); /* Open file stream */
fscanf(f1, "%d\t%d", &m, &n); /* Get dimension of matrix */
printf("Rows: %d\tColumns: %d\n",m,n); /* Print dimensions to command line for verification */
double A[m][n]; /* Create empty matrix */
for (int i = 0; i < m; i++) { /* Load matrix from file to matrix A */
for (int j = 0; j < n; j++) {
fscanf(f1, "%d", &matnum1); /* Get elements from file */
A[i][j] = matnum1; /* Write element to A */
}
}
printf("Matrix has been loaded\nDisplaying matrix:\n"); /* Print elements of matrix A to command line to verify program works correctly */
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matele = A[i][j];
printf("%d\n",matele);
}
}
return 0;
}
I do include but for some reason that doesn't show when I copied the code into here. Thanks for all the help!
Jeff
file not opened?
Another wild guess: When you open the file using
f1 = fopen(inputname, "r");
you don't check whether the file really could be opened - f1 could be a pointer to NULL as well. Maybe fscan'ning something from nowhere causes a segfault (don't know, have never tried it :) When you open your Application in the Finder, the input file is probably not in your path, which could trigger your bug.
Mind that I'm just guessing here, I didn't try your code. Just out of curiosity: Is your HPC class really plain C (i.e. you are not allowed to use C++)? Just asking, because I always found that printf/scanf are really a PITA compared to C++'s stream IO.
Thanks for the response. The
Thanks for the response. The files are always in the same folder as the executable so I don't think it is a path issue. I know I should check to see if the file could be opened but this was more of an exercise type program that I wasn't going to use frequently.
As for the HPC class, I just emailed the professor and asked her what language(s) we would be using. She told me C/Fortran and MPI. I definitely agree with you that C++ is better at stream IO (I assume that is what you meant - I have no idea what PITA means). Thanks
Jeff
Solution.
This is indeed a path problem, you see whenever you run an exec from Finder the current path is going to be:
/users/yourUsername/
So all you have to do is either put your reading files on that path or change directory everytime you run your exec.
See, just try to put this at the beginning of your program to see what happens, it will show the current path the exec is running on:
system("pwd");
Btw, always check for your file to be opened, that's why it was crashing, maybe this xkcd cartoon will help you remind good practice ;)
http://imgs.xkcd.com/comics/goto.png
Hope this was useful.