Like Tree3Likes

I changed my code and recompiled with gcc/makefile, but nothing changed???

This is a discussion on I changed my code and recompiled with gcc/makefile, but nothing changed??? within the C Programming forums, part of the General Programming Boards category; I suspected this was happening (its happened before), so I modified the following line of code in input.c: Code: else{printf("Invalid ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    Angry I changed my code and recompiled with gcc/makefile, but nothing changed???

    I suspected this was happening (its happened before), so I modified the following line of code in input.c:

    Code:
    else{printf("Invalid Command!");}
    to

    Code:
    else{printf("!");}
    I then removed all the .o files, including input.o, as well as the compiled .exe

    Code:
    x:	
    	rm c c.exe main.o input.o main input check4check.o makemove.o dangergenerator.o printdangerboard.o absolutepins.o convertfen.o printboard.o countpieces.o pawnmoves.o bishopmoves.o rookmoves.o knightmoves.o queenmoves.o kingmoves.o createmove.o displaymoves.o
    and I verified everything was removed from my directory except for the .c and .h files

    I then recompiled my program and ran it, and the output wasn't changed at all, because it still says "Invalid command!" -- how is this happening? I am changing and saving the .c file and recompiling, and it is using the old code that as far as I can tell should no longer exist anywhere. This makes it impossible to debug since I can't actually change the code. The .c file has definitely changed, but it is clearly not actually recompiling input.c, and somehow it is reusing the old code that I've deleted.

    Here is my makefile:

    Code:
    c:	main.o input.o check4check.o makemove.o dangergenerator.o printdangerboard.o absolutepins.o convertfen.o printboard.o countpieces.o pawnmoves.o bishopmoves.o rookmoves.o knightmoves.o queenmoves.o kingmoves.o createmove.o displaymoves.o 
    	gcc -g main.o input.o check4check.o makemove.o dangergenerator.o printdangerboard.o absolutepins.o convertfen.o printboard.o countpieces.o pawnmoves.o bishopmoves.o rookmoves.o knightmoves.o queenmoves.o kingmoves.o createmove.o displaymoves.o -o c
    
    main.o:			main.c header.h
    input.o:			input.c header.h
    convertfen.o:			convertfen.c header.h
    printboard.o:			printboard.c header.h
    countpieces.o:		countpieces.c header.h
    pawnmoves.o:			pawnmoves.c header.h
    bishopmoves.o:		bishopmoves.c header.h
    rookmoves.o:			rookmoves.c header.h
    knightmoves.o:		knightmoves.c header.h
    queenmoves.o:			queenmoves.c header.h
    kingmoves.o:			kingmoves.c header.h
    createmove.o:			createmove.c header.h
    displaymoves.o:		displaymoves.c header.h
    absolutepins.o: 		absolutepins.c header.h
    dangergenerator.o:		dangergenerator.c header.h
    printdangerboard.o:		printdangerboard.c header.h
    makemove.o:			makemove.c header.h
    check4check.o:		check4check.c header.h
    
    x:	
    	rm c c.exe main.o input.o main input check4check.o makemove.o dangergenerator.o printdangerboard.o absolutepins.o convertfen.o printboard.o countpieces.o pawnmoves.o bishopmoves.o rookmoves.o knightmoves.o queenmoves.o kingmoves.o createmove.o displaymoves.o
    
    xx:
    	rm c
    Last edited by Adam Rinkleff; 06-23-2011 at 03:07 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I guess what I'm trying to do is force a clean compile.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adam Rinkleff View Post
    Here is my makefile:
    Something is wrong with your story, because these recipes:

    Code:
    main.o:			main.c header.h
    input.o:			input.c header.h
    convertfen.o:			convertfen.c header.h
    printboard.o:			printboard.c header.h
    Do not make anything. In other words, using this makefile will never produce any of those .o files, so it could never produce the first target, either, which requires them.

    A recipe needs more than prerequisites. Eg, this:

    Code:
    main.o: main.c header.h
        gcc -Wall -g main.c
    Will compile main.o, because it uses the compiler. If you do not use the compiler in a target recipe, you are not compiling anything.

    In a nutshell: you are mistaken to believe that your makefile accomplishes anything at all, which might explain why your executable is not updated.
    Last edited by MK27; 06-23-2011 at 03:53 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by MK27 View Post
    these recipes do not make anything. In other words, using this makefile will never produce any of those .o files
    Um, well, it does. There is nothing else in there to make anything, and when I type make, it certainly makes a bunch of .o files. I'm really not sure what you are trying to say, this is the same makefile I used to get the program to compile in the first place. I've never used anything else on it. I might point out that I've been deleting the executable, and recompiling it. Its coming from somewhere -- from the makefile.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    Actually, make has some pretty fancy implicit rules, and will work just fine if you don't give it a compile command. I don't see anything immediately wrong with his makefile, though it's not a very pretty one.

    @Adam Rinkleff:
    Are you sure that is the only instance of "Invalid Command!" in your entire program, or that you changed the instance you think you changed? Try grep "Invalid Command" *.[ch], and make sure there's only one instance in there. Before you do a "make c", make sure that there are no .o files or executables in there that you can link with. Remove them by hand if you need to, and check by running "ls".


    Here might be a better makefile. You might need to tweak this a bit, but it's a far cry better than what you have. I hope the explanations in the comments are clear enough.
    Code:
    CC = gcc
    CFLAGS = -c -Wall # add any custom include paths, etc
    LDFLAGS =  #put in any custom library paths or required libs, etc here
    INCS = $(wildcard *.h)
    SRCS = $(wildcard *.c)
    OBJS = $(patsubst %.c, %.o, $(SRCS))
    EXE = chess
    
    
    default:    $(EXE)  # this way, if you just type "make", it will make your chess executable
    
    
    %.o : %.c $(INCS)  # any file ending in .o requires it's corresponding .c file and everything in INCS
        $(CC) $(CFLAGS) -o $@ $<  # compile using CC, with CFLAGS flags, the output file is named $@, the target name (what matches %.o), and the input file is $< (the first prerequisite, what matches the %.c)
    
    
    $(EXE) : $(OBJS)  # the target 'chess' is dependent on all the object files
        $(CC) $(LDFLAGS) -o $@ $^  # compile using CC, with LDFLAGS flags, the output file is named $@, the target name (chess), and the input file is $^, the entire list of prerequisites
    
    
    clean:
        rm -f $(OBJS) $(EXE)  # remove all object files and the executable
    
    
    clean_exe:
        rm -f $(EXE)  # remove the executable only
    Note, there are better ways (i.e. dynamic) to determine what header files a .c file depends on, but for your smallish program, just making every .c rely on every .h should suffice, especially since it seems like there is only one .h file (header.h).

    EDIT: Also note, make is very particular about white space. Make sure you use no indentation before the target : prereq lines, and a single tab before any commands. Leave a blank line between targets to be safe.
    Last edited by anduril462; 06-23-2011 at 04:03 PM.
    MK27 likes this.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Although I have no idea what -wall or why you thought it was necessary, I went ahead and tried this, which doesn't compile at all and gives the error "compilation of header requested"

    Code:
    c:	main.o input.o check4check.o makemove.o dangergenerator.o printdangerboard.o absolutepins.o convertfen.o printboard.o countpieces.o pawnmoves.o bishopmoves.o rookmoves.o knightmoves.o queenmoves.o kingmoves.o createmove.o displaymoves.o 
    	gcc -Wall -g main.o input.o check4check.o makemove.o dangergenerator.o printdangerboard.o absolutepins.o convertfen.o printboard.o countpieces.o pawnmoves.o bishopmoves.o rookmoves.o knightmoves.o queenmoves.o kingmoves.o createmove.o displaymoves.o -o c
    
    main.o:			main.c header.h
    	gcc -Wall -g main.c header.h
    input.o:                      input.c header.h
    	gcc -Wall -g input.c header.h
    convertfen.o:		convertfen.c header.h
    	gcc -Wall -g convertfen.c header.h
    printboard.o:		printboard.c header.h
    	gcc -Wall -g printboard.c header.h
    countpieces.o:		countpieces.c header.h
    	gcc -Wall -g countpieces.c header.h
    pawnmoves.o:		pawnmoves.c header.h	
    	gcc -Wall -g pawnmoves.c header.h
    bishopmoves.o:		bishopmoves.c header.h
    	gcc -Wall -g bishopmoves.c header.h
    rookmoves.o:		rookmoves.c header.h
    	gcc -Wall -g rookmoves.c header.h
    knightmoves.o:		knightmoves.c header.h
    	gcc -Wall -g knightmoves.c header.h
    queenmoves.o:		queenmoves.c header.h
    	gcc -Wall -g queenmoves.c header.h
    kingmoves.o:		kingmoves.c header.h
    	gcc -Wall -g kingmoves.c header.h
    createmove.o:		createmove.c header.h
    	gcc -Wall -g createmove.c header.h
    displaymoves.o:		displaymoves.c header.h
    	gcc -Wall -g displaymoves.c header.h 
    absolutepins.o: 	absolutepins.c header.h	
    	gcc -Wall -g absolutepins.c header.h
    dangergenerator.o:	dangergenerator.c header.h
    	gcc -Wall -g dangergenerator.c header.h
    printdangerboard.o:	printdangerboard.c header.h
    	gcc -Wall -g printdangerboard.c header.h
    makemove.o:		makemove.c header.h
    	gcc -Wall -g makemove.c header.h
    check4check.o:		check4check.c header.h
    	gcc -Wall -g check4check.c header.h
    Last edited by Adam Rinkleff; 06-23-2011 at 04:05 PM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by anduril462 View Post
    Are you sure that is the only instance of "Invalid Command!" in your entire program, or that you changed the instance you think you changed?
    100% positive. Its a small program, I just wrote it, its the only file in the program that deals with input, and the only possible place that the line would be coming from. I know for certain, which is why I changed it in the first place to test.

    Quote Originally Posted by anduril462 View Post
    Before you do a "make c", make sure that there are no .o files or executables in there that you can link with.
    Did that too.

    Quote Originally Posted by anduril462 View Post
    Here might be a better makefile.
    I'll try it, but it looks way over my head. I don't have a clue how to get that to work with my files. I just learned what a makefile was yesterday.
    Last edited by Adam Rinkleff; 06-23-2011 at 04:07 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adam Rinkleff View Post
    Um, well, it does. There is nothing else in there to make anything, and when I type make, it certainly makes a bunch of .o files.
    Hey, my apologies, I just tried this and you are right, that is what happens, this is a make feature I was unaware of.

    However, while I can reproduce that behaviour I can't reproduce the problem. So I am stumped now too.

    One suggestion I do have is that you use a - with rm so it does not crap out on the first non-existent file:

    Code:
         -rm *.o etc
    Since without the - rm will stop after trying to remove the first non-existent file.

    Also, you shouldn't include the .h in the compiler calls (post #6).
    Last edited by MK27; 06-23-2011 at 04:11 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    Quote Originally Posted by Adam Rinkleff View Post
    Although I have no idea what -wall or why you thought it was necessary, I went ahead and tried this, which doesn't compile at all and gives the error "compilation of header requested"
    The flag is -Wall. I always slip it in as a suggestion, since it enables all warnings and ensures you are writing good, clean code. Maybe not something you want to deal with now, but I suggest throwing it in once you sort out your make issues. As for the "compilation of header", take a look:
    Code:
    gcc -Wall -g main.c header.h
    You're trying to compile a header file. Drop header.h from each of those lines. gcc's preprocesser will pick it up from the #include directive in your .c file.

    Also, there's a small chance the time stamps are somehow off on your files. Right after you clean everything, do a "ls -l" and look at the time stamps, then compare it with what you get from running "date". Alternatively, update the time stamps for all your .c files with "touch *.c".
    rags_to_riches likes this.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    For reference, Make has great documentation (GNU `make'), including discussing all the implicit rules, automatic variables, etc, and great examples in there too.
    MK27 likes this.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Ok here is something really weird that just happened. I've been trying to use script to show you the problem, and its been really hard to capture -- just now I compiled the program and ran it, and it wasn't working properly. Then -all- I did was type script, and then restarted the program -without- recompiling, and suddenly it worked just fine. The first time I displayed the wrong error message, but after typing script it displayed the correct error message. Note that since it has worked, and then not worked, and then worked -- I've been switching the error messages back and forth. Since I didn't recompile anything or change anything, this really doesn't make sense. There isn't some variable at work here, because the issue involves a printf statement which either prints what is saved there, or prints what was saved there but has long since been deleted. I feel like there are somehow two different versions of my code being saved. Just to try this again, I exited and ended the script, and once again the program failed to operate correctly using the old code -- then I started the script, and suddenly it was working perfectly again! Here is what was on my screen:


    ajr0112@csp10: ./c
    ./c

    ! <----This is the line of code acting improperly, at this point it should say "Invalid input" Note the wierd space between ./c and !, this should not be there. It is clearly reading the input wrong and not running the functions as it should.


    Enter Command <h=help>:
    [1]+ Stopped ./c
    ajr0112@csp10:
    ajr0112@csp10: script
    Script started, file is typescript <----- To show you this, I started a script:
    csp10$ ./c
    rnbkqbnr <------All this is what should have shown previously where there was just a wierd line of space
    pppppppp




    PPPPPPPP
    RNBKQBNR


    Pieces in Play (W-B)
    Pawns: 8-8
    Knights: 2-2
    Bishops: 2-2
    Rooks: 2-2
    Queens: 1-1
    Kings: 1-1

    Total Legal Moves: 20
    ||0||0...a5
    0...a6
    0...b5
    0...b6
    0...c5
    0...c6
    0...d5
    0...d6
    0...e5
    0...e6
    0...f5
    0...f6
    0...g5
    0...g6
    0...h5
    0...h6
    0...Na6
    0...Nc6
    0...Nf6
    0...Nh6


    Enter Command <h=help>: sd
    Invalid inputsd <------------- There it is, invalid input is displaying correctly!


    Enter Command <h=help>: exit
    Invalid inputexit <------------- There it is, invalid input is displaying correctly!


    Enter Command <h=help>:
    [1]+ Stopped ./c <------------- I exit
    csp10$
    csp10$ exit
    exit
    There are stopped jobs.
    csp10$ exit
    exit
    Script done, file is typescript <------------- I end scripting
    ajr0112@csp10: ./c
    ./c
    <------------- Once again the program acts improperly, showing a blank line of space and then an ! instead of "invalid input"
    !


    Enter Command <h=help>: s
    !s <-------- This makes it more clear to you that the ! is the error message for bad input, which was previously in the code but should now be replaced by "invalid input" also note that the previous ! was generated before enter command was ever output, because for some reason the original enter command is never being displayed


    Enter Command <h=help>:
    [1]+ Stopped ./c <-------------- I end the program
    ajr0112@csp10: script <------------- I start scripting back up again
    Script started, file is typescript
    csp10$ ./c
    rnbkqbnr <-------------- The program is working perfectly
    pppppppp




    PPPPPPPP
    RNBKQBNR


    Pieces in Play (W-B)
    Pawns: 8-8
    Knights: 2-2
    Bishops: 2-2
    Rooks: 2-2
    Queens: 1-1
    Kings: 1-1

    Total Legal Moves: 20
    ||0||0...a5
    0...a6
    0...b5
    0...b6
    0...c5
    0...c6
    0...d5
    0...d6
    0...e5
    0...e6
    0...f5
    0...f6
    0...g5
    0...g6
    0...h5
    0...h6
    0...Na6
    0...Nc6
    0...Nf6
    0...Nh6


    Enter Command <h=help>: ff
    Invalid inputff


    -------------------------------------------------
    So without recompiling anything, I have two completely different versions of the program running, depending on whether or not script is running, with the older version running very incorrectly. I don't think my problem has anything to do with my code or with the makefile. Something else is going on here.
    Last edited by Adam Rinkleff; 06-23-2011 at 05:00 PM.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    This makes no sense. All script does is record what I type, so how can it affect not only the functioning of my code, but also affect which version of that code is run? Somehow, it is pulling a deleted version out of thin air!

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,513
    Nah...it's just acting like it pulled a deleted version out of thin air. You have something screwy going on in your environment somehow. Without all your code, it's really hard to tell exactly what's going on since I can't replicate anything. What I can recommend however, is:
    1. Get rid of all background processes that you put to sleep
    2. Stop using script and make sure you've exited all script sessions.
    3. Open up a new terminal session, so you have a clean set of environment variables, in case any of those got messed up somehow.
    4. Clean up all object files and the executable.
    5. Add a -g flag to all your compilations to put debug symbols in the executable.
    6. Remake the application
    7. Run it in GDB, setting a breakpoint at main and stepping through, inspecting every line of code as you go.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I really don't think its the code, this has been happening even if I have a simple hello world application and try to modify it. And its a university computer, so you would think the environment would be pretty stable. I wonder if there i some kind of conflict with winscp.
    Last edited by Adam Rinkleff; 06-23-2011 at 05:24 PM.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    This is really wierd, it only works when script is active. What does script do that would change things?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Still get error even if i changed a lot..
    By icefire99 in forum C++ Programming
    Replies: 9
    Last Post: 04-26-2007, 03:31 AM
  2. Website changed...
    By funkydude9 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-04-2003, 10:44 PM
  3. How times have changed
    By golfinguy4 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-21-2002, 11:25 PM
  4. Not much has changed in four years...
    By Eibro in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-18-2002, 08:11 PM
  5. How has this array been changed?
    By Agha in forum C Programming
    Replies: 4
    Last Post: 04-09-2002, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21