Problem in compiling a C program in terminal

Discussion in 'Mac Programming' started by uipe, Nov 13, 2007.

  1. macrumors newbie

    Joined:
    Nov 13, 2007
    #1
    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 :confused: any tip?

    ps: the macbook really rules :D
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    It might help if you embellished your issue with some details.

    Todd
     
  3. thread starter macrumors newbie

    Joined:
    Nov 13, 2007
    #3
    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
    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.
     
  4. macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #4
    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.
     
  5. thread starter macrumors newbie

    Joined:
    Nov 13, 2007
    #5
    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 ?
     
  6. macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #6
    It was probably random that it seemed to be fine under certain circumstances. Behavior is not entirely predictable when you make mistakes with memory.
     

Share This Page