cost
cost

Reputation: 4480

C gcc compilation question and makefiles

Not even quite sure what my question is. The short of it is, for a class I'm supposed to add some functionality to this c file, and it came with a handy makefile.

CFLAGS=-DUNIX -lreadline -lcurses -ansi -pedantic-errors
DEBUG=-g
#DEBUG=

all: shell

shell:  shell.c parse.c parse.h
    gcc $(CFLAGS) $(DEBUG) shell.c parse.c -o shell
clean:
    rm -f shell *~

I have to add features to shell.c. I'm very new to C (usually use c++ or c#) so I'm testing out little things in a separate little tests.c file. Things like, see what exactly certain system calls return, how to printf them right, etc. Anyway, tests.c seems to be conforming to different c compiler standards or I'm compiling it wrong. If I accidentally use // to comment something out or declare a variable somewhere other than at the start in shell.c, the compiler yells at me. It doesn't care in tests.c.

I compile tests.c with "gcc tests.c -o tests" If I compile the shell using "gcc shell.c parse.c -o shell" it compiles fine, but running it simply gives me a segmentation fault. I would love to ask my TA about this, but every time I as him something he answers a completely different question...

Any thoughts on what's going on here? Perhaps a point in the right direction at least?

Upvotes: 0

Views: 1410

Answers (4)

chrisaycock
chrisaycock

Reputation: 37920

The -ansi -pedantic-errors prevents the impurities like // and variable definitions in the middle of the function. Remove that and you should be able to sin away.

As for the segmentation fault, your best bet is to run your program through gdb to see where it crashes.

Upvotes: 1

Juliano
Juliano

Reputation: 41367

The problem is that your makefile includes -ansi -pedantic-errors flags for the compiler. This forces it to use a very old version of C. Perhaps this Makefile was provided by your instructor and he wants like that? It is not uncommon.

To use these new features (// comments, automatic variables anywhere in a block) just drop these two flags. If you have the freedom, I recommend also using -std=c99 -Wall.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

To get GCC to accept C99 conventions, tell it to do so:

gcc -std=c99 ...
gcc -std=gnu99 ...

So, add -std=gnu99 to your CFLAGS value, and remove -ansi which is equivalent to -std=c89. If you must code to C89 standards, do not use // comments.

We can't tell what causes the core dump - but it could be that you're trying to modify a string literal somewhere, or any of a large number of other problems.

Upvotes: 1

bta
bta

Reputation: 45057

Why are you compiling by calling gcc directly instead of using the makefile? The makefile adds a number of additional command-line gcc options which are most likely important. Do you see the same behavior if you compile using make all?

Since you are new to C, I would recommend adding -Wall to the CFLAGS line. This will enable all compiler warnings, which may alert you to a subtle error that you might have otherwise missed.

Upvotes: 0

Related Questions