Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 13, 2007, 09:11 AM   #1
uipe
macrumors newbie
 
Join Date: Nov 2007
Problem in compiling a C program in terminal

Hi,

I am a new mac user , I just recently bought my macbook for school work!

My ploblem is that when I compile a C program in the terminal using a Makefile that I created and tested in linux, the program compiles correctly but it segfaults while running, but if I compile it in xcode it works grate any tip?

ps: the macbook really rules
uipe is offline   0 Reply With Quote
Old Nov 13, 2007, 09:25 AM   #2
toddburch
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
It might help if you embellished your issue with some details.

Todd
toddburch is offline   0 Reply With Quote
Old Nov 13, 2007, 09:50 AM   #3
uipe
Thread Starter
macrumors newbie
 
Join Date: Nov 2007
oki, sorry

Makefile :

Code:
#flags for the compiler
CFLAGS = -Wall -W -g -Wmissing-prototypes

# Libs
LIBS = -lm -lpthread


#++++++++++++++++  files  +++++++++++++++++++++
OBJS = main.o cmdmatrix.o

# exec name
PROGRAM = matrix
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 

# make principal
all: ${PROGRAM}

# compile with debug
debug: CFLAGS += -D SHOW_DEBUG 
debug: ${PROGRAM}

${PROGRAM}: ${OBJS}
	${CC} -o $@ ${OBJS} ${LIBS}


#++++ Dependencies +++++
main.o: main.c cmdmatrix.h
cmdmatrix.o: cmdmatrix.c cmdmatrix.h
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



# convert .c in .o
.c.o:
	${CC} ${CFLAGS} -c $<
here is the main.c
Quote:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <ctype.h>
#include <math.h>

#include "cmdmatrix.h"
#define TAMANHO 200

"..."
static struct gengetopt_args_info params;
struct tm *tempoUtc;
time_t tempo;
char titulo[TAMANHO]="";
char tempoTitulo[20];

"..."
//printf("# matrix – desempenho\n# Parâmetros da execução: ");
strcat(titulo,"# matrix – desempenho\n# Parâmetros da execução:");
if(params.dimensao_given) {
//printf("-n %s ", params.dimensao_orig);
strcat(titulo," -n ");
strcat(titulo,params.dimensao_orig);
}else {
strcat(titulo," -m ");
strcat(titulo,params.minimo_orig);
strcat(titulo," -M ");
strcat(titulo,params.maximo_orig);
strcat(titulo," -p ");
strcat(titulo,params.passo_orig);
} //printf("-m %s, -M %s, -p %s ", params.minimo_orig, params.maximo_orig, params.passo_orig);
if(params.repeat_given){
strcat(titulo," -x ");
strcat(titulo,params.repeat_orig);
//printf("-x %s ",params.repeat_orig);
}
if(params.teste_given) {
strcat(titulo," -t ");
strcat(titulo,params.teste_orig);
//printf("-t %s ",params.teste_orig);
}
if(params.ficheiro_given) {
strcat(titulo," -e ");
strcat(titulo,params.ficheiro_orig);
//printf("-e %s ",params.ficheiro_orig);
}
if(params.grafico_given) {
strcat(titulo," -g ");
strcat(titulo,params.grafico_orig);
//printf("-g %s ",params.grafico_orig);
}
if(params.aviso_given) {
strcat(titulo," -i");
//printf("-i");
}

// cria var com tempo actual tempo actual
tempo = time (NULL);
// formata o tempo em UTC
tempoUtc = localtime (&tempo);
sprintf(tempoTitulo,"\n# @%d-%d-%d %dh%d\n",(tempoUtc->tm_year+1900),tempoUtc->tm_mon,tempoUtc->tm_mday,tempoUtc->tm_hour,tempoUtc->tm_min);
strcat(titulo,tempoTitulo); //<---- PROBLEM
strcat(titulo,"# Dimensao: Numero de execucoes: tempo media (s): desvio-padrao (s)\n");
printf("%s",titulo);
"..."
I have run it in gdb and the segmentation fault is here strcat(titulo,tempoTitulo); and if I tell gdb to print titulo it says its repeats 145 times.

Again, this program is working well in Linux and if compiled in Xcode 3.0, I am using Leopard.
uipe is offline   0 Reply With Quote
Old Nov 13, 2007, 10:05 AM   #4
Gelfin
macrumors 68020
 
Gelfin's Avatar
 
Join Date: Sep 2001
Location: San Francisco, CA
tempTitulo is only 20 characters in length and you are trying to write more than that into the string on the line above. Make sure your string is long enough to hold the longest possible value, including a terminating NULL.
__________________
Reality is that which, when you stop believing in it, doesn't go away.
--Philip K. Dick
Gelfin is offline   0 Reply With Quote
Old Nov 13, 2007, 02:31 PM   #5
uipe
Thread Starter
macrumors newbie
 
Join Date: Nov 2007
thanks a lot it worked, to be honest I was really sceptic when I tested, because I just dont understand why it worked with xcode and linux :S and not when compiled with make in terminal, any ideas ?
uipe is offline   0 Reply With Quote
Old Nov 13, 2007, 04:12 PM   #6
Gelfin
macrumors 68020
 
Gelfin's Avatar
 
Join Date: Sep 2001
Location: San Francisco, CA
Quote:
Originally Posted by uipe View Post
thanks a lot it worked, to be honest I was really sceptic when I tested, because I just dont understand why it worked with xcode and linux :S and not when compiled with make in terminal, any ideas ?
It was probably random that it seemed to be fine under certain circumstances. Behavior is not entirely predictable when you make mistakes with memory.
__________________
Reality is that which, when you stop believing in it, doesn't go away.
--Philip K. Dick
Gelfin is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Problems running program in Terminal after compiling in Xcode. Flint-Paper Mac Programming 2 May 18, 2011 04:47 AM
256 colors in Terminal.app ChrisBuchholz Mac Applications and Mac App Store 0 Nov 24, 2010 03:52 AM
Help compiling/running graphics in terminal dflem11 Mac Programming 2 Nov 8, 2010 02:42 PM
Compiling an Assembly program with Terminal applefanDrew Mac Programming 2 Sep 13, 2010 04:57 PM
running c program in Terminal rmumma2 Mac Programming 6 Mar 15, 2008 06:53 PM


All times are GMT -5. The time now is 08:46 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC