Building and debugging a program with makefile - c

As part of my university studies I learn C programming. I'm using Eclipse Juno IDE.
Here is the problem: I have few .c and .h files, and a makefile made by our course staff. I want to build the project with this makefile. I spent hours searching all over the internet how to build and debug it, but everytime I think I got it an error jumps.
Can someone please explain once and for always how to do this process - build the project with the given makefile, and run it in debug mode so I can debug it?
It is important to mention that sometimes I failed to build the project and got the following message:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file Assignment2altVersion.exe: Permission denied
However, I managed to build the project several times, but when I tried to run it in debug mode it was stuck either on "Launching: Configuring GDB" or "Launching C/C++ Application".
EDIT:
there are 6 files: SP_Stack.c, SP_Stack.h, SP_Aux.c, SP_Aux., SP_Stack_UnitTest.c, main.c
SP_Aux.c and main.c #include SP_Aux.h.
SP_Stack.c, SP_Aux. and SP_Stack_UnitTest.c #include SP_Stack.h
here's the makefile:
ex2: main.o SP_Aux.o SP_Stack.o
gcc -std=c99 -Wall -Werror -pedantic-errors main.o SP_Aux.o SP_Stack.o -o ex2
Stack_UnitTest: SP_Stack.o SP_Stack_UnitTest.o
gcc -std=c99 -Wall -Werror -pedantic-errors SP_Stack.o SP_Stack_UnitTest.o -o Stack_UnitTest
main.o: main.c SP_Aux.h SP_Stack.h
gcc -std=c99 -Wall -Werror -pedantic-errors -c main.c
SP_Stack.o: SP_Stack.c SP_Stack.h
gcc -std=c99 -Wall -Werror -pedantic-errors -c SP_Stack.c
SP_Stack_UnitTest.o: SP_Stack.h SP_Stack_UnitTest.c
gcc -std=c99 -Wall -Werror -pedantic-errors -c SP_Stack_UnitTest.c
SP_Aux.o: SP_Aux.c SP_Aux.h
gcc -std=c99 -Wall -Werror -pedantic-errors -c SP_Aux.c
clean:
rm -f main.o SP_Aux.o SP_Stack.o SP_Stack_UnitTest.o Stack_UnitTest ex2
the 6 files I mentioned and the makefile are all in the same directory.
I just tried it all over again: went to the right side of the screen to the "make targets" tab. I chose my project and created a target named "makefile" with "make" as build command. then I double clicked it and the project finishe building. when I tried to run it in debug mode it gets stuck on 96% with a message: "Launching: configuring GDB"

Related

Compilation error on linux server (C project)

I need some help.
I worked on a C project locally and it ran perfect with no issues at all.
Then I moved my whole project files to a linux server (using Bitwise) and ran it using the following command:
gcc -g -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c map.c map.h utilities.c
utilities.h election.c election.h extended_map.c extended_map.h test_utilities.h -o outmap
and again everything worked as expected.
Now, I want to replace my version of test_utilities.h with the version saved on that server, so I opened main.c (which is the only file to include test_utilities.h and replaced:
#include "test_utilities.h"
with
#include "~mtm/public/1920b/ex1/test_utilities.h"
But the terminal shows me the following error:
gcc: error: test_utilities.h: No such file or directory
-bash-4.2$
As suggested I changed it to
gcc -g -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c map.c map.h utilities.c utilities.h election.c election.h extended_map.c extended_map.h test_utilities.h -o outmap -I ~mtm/public/1920b/ex1/
But still I get the following:
gcc: error: test_utilities.h: No such file or directory
update2: (I was requested to remove .h files so now I got)
gcc -g -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c map.c utilities.c election.c extended_map.c -o outmap
Writing ~mtm to refer to the home directory of user mtm is a shortcut that your shell understands. It isn't something that the C preprocessor understands. So you'll have to spell it out as /home/mtm (or wherever mtm's home directory is located) instead of ~mtm.
That said, a better way would be to just leave it as "test_utilities.h" and instead adjust the include path of the compiler (specified via -I when invoking the compiler) to include ~mtm/public/1920b/ex1/.
You also shouldn't specfiy test_utilities.h as an argument to the compiler. In fact none of the header files should be passed as arguments to the compiler.

Makefile: how to specify library name at the end of command using patsubst

I am new to Makefile, C and Linux. I am using gcc & ubuntu. I encountered a problem while trying to compile the code with a link to a library.
Here is my problem. I have:
a_tests.c & b_tests.c files in "tests" folder
lib.a file in "build" folder
Here is the codes in Makefile related to the problem:
CFLAGS=-g -O2 -Wall -Wextra -Isrc -DNDEBUG $(OPTFLAGS)
TARGET=build/lib.a
TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))
tests: CFLAGS += $(TARGET)
tests: $(TESTS)
When the tests files are compiled, "undefined reference to 'XXXfunction'"errors will be prompted. Because what executed behind is
gcc -g -O2 -Wall -Wextra -Isrc -DNDEBUG build/lib.a tests/a_tests.c -o test/a_tests
gcc -g -O2 -Wall -Wextra -Isrc -DNDEBUG build/lib.a tests/b_tests.c -o test/b_tests
But "build/lib.a" should be placed after the output file name to solve it (If I manually type in the below commands, the codes would be successfully compiled), ie:
gcc -g -O2 -Wall -Wextra -Isrc -DNDEBUG tests/a_tests.c -o test/a_tests build/lib.a
gcc -g -O2 -Wall -Wextra -Isrc -DNDEBUG tests/b_tests.c -o test/b_tests build/lib.a
But I don't know how to change in the Makefile, I tried -l -L options, they didn't work. It would warn that "cannot find the .a file". Any help would be appreciated. Thank you so much in advance!
Define the library as a dependency, because it is one. It will be appended at the end of the other dependencies, here: the source.
CFLAGS=-g -O2 -Wall -Wextra -Isrc -DNDEBUG $(OPTFLAGS)
TARGET=build/lib.a
TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(TEST_SRC:%.c=%)
tests: $(TESTS)
$(TESTS): $(TARGET)
The library does not need to be after the output file, but after the depending input file.
The makefile is further simplified:
Replaced the patsubst with a simpler expression.
Moved the target tests up, so it is found as the first and therefore default target.
Good luck!

"curses.h: No such file or directory" even after installing into Cygwin

I'm working on a C clone of the 2048 game, using curses.h for the UI. When trying to compile it with Cygwin using the make commanad, I get following message:
PS D:\C\ps3> make all
gcc -std=c11 -Wall -Werror -g -c main.c -lm -lcurses -o main.o
main.c:4:20: fatal error: curses.h: No such file or directory
#include <curses.h>
^
compilation terminated.
make: *** [Makefile:13: main.o] Error 1
So I ran the setup again, looking for any package that has "curses" in it's name and installed it, added my /bin folder to the PATH variable but it didn't help.
I'm working on a 64-bit Win10 and trying to compile the program with Cygwin's terminal, using a Makefile. file. I've tried reinstalling the packages with curses in their name multiple times with no help.
Part of my Makefile:
CC=gcc
CFLAGS=-std=c11 -Wall -Werror -g
LDLIBS=-lm -lcurses
OUTPUT=game
# targets
all: $(OUTPUT)
$(OUTPUT): k.o hof.o main.o
$(CC) $(CFLAGS) k.o hof.o main.o $(LDLIBS) -o $(OUTPUT)
main.o: main.c
$(CC) $(CFLAGS) -c main.c $(LDLIBS) -o main.o
The line in main.c the error is pointing to:
#include "hof.h"
#include "k.h"
#include "ui.h"
#include <curses.h>
The header file would be in libncurses-devel (perhaps overlooked). Here's a screenshot showing the "curses" packages which I have in my local repository:

How to build CS50 programs with make

I am trying to study cs50 on linux , I downloaded everything I found on github, but now I can not compile my first program with make, but I can use clang instead clang hello.c -lcs50 -o hello which works just fine, but when I try to compile with make hello I get
:~/cs50/pset1# make hello
cc hello.c -o hello
/usr/bin/ld: /tmp/cczILfhu.o: in function 'main':
hello.c:(.text+0x1a): undefined reference to 'get_string'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: hello] Error 1
I even moved the libcs50 folder that I downloaded to /usr/include/
but I still get the same results.
after I compile with clang , and then excute make hello it says
make: 'hello' is up to date.
I know it sounds dump but I am still newbie and looking for help.
thanks in advance.
For linking in the cs50 library (which you should have installed from https://github.com/cs50/libcs50 according to the instructions there), your linking command should specify the -lcs50 argument.
make usually needs a Makefile to control the build. In its absence it can use some implicit rules to guess the build process, like that hello.o could be built from hello.c and hello could be linked from hello.o and so forth, but it certainly cannot guess that libcs50 should be linked in.
Fortunately, the implicit linking rules include the contents of the variable LDLIBS in the correct, so you can fix this by writing a simple Makefile in the same directory, containing just
LDLIBS += -lcs50
I.e. "append the string -lcs50 to the current value of LDLIBS".
After that make hello will use the implicit rules and the new value of LDLIBS to execute
cc hello.c -lcs50 -o hello
Also do note that the cc command usually is GCC, not Clang, not that it should matter in CS50. It can be configured with the CC variable in Makefile:
CC := clang
Finally, it does make sense to enable warnings and pedantry in the compilation flags, for example:
CFLAGS += -Wall -Wextra -Werror -pedantic -std=c11
With all these 3 present, make hello will actually execute
clang -Wall -Wextra -Werror -pedantic -std=c11 hello.c -lcs50 -o hello
which means we did save quite a lot typing and get more useful diagnostics!
Of course for a more complicated build process you'd need to write a more complicated Makefile with dependency rules - say if your helloworld program consisted of hello.c and world.c linked together you could get by the implicit rules and just state that helloworld depends on both hello.o and world.o and should be linked together from these:
helloworld: hello.o world.o
$(CC) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS)
# the command *must* be indented by a *single* tab character, not spaces!
# unfortunately SO editor does not make it easy to write tabs.
Just make new Makefile in the dir where is your *.c file:
$ touch Makefile
Then just add this strings to your Makefile:
CC=clang
CFLAGS=-fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow
LDLIBS=-lcrypt -lcs50 -lm
Than you can compile *.c file just typing:
$ make hello.c

Argon2 Makefile is throwing an error (Windows)

I am trying to use argon2 for C. I cloned the repo and tried to build it myself by typing make in the root directory of the repo. However, make threw this error:
Building without optimizations
cc -std=c89 -O3 -Wall -g -Iinclude -Isrc -pthread src/argon2.c src/core.c
src/blake2/blake2b.c src/thread.c src/encoding.c src/ref.c src/run.c -o argon2
process_begin: CreateProcess(NULL, cc -std=c89 -O3 -Wall -g -Iinclude -Isrc -
pthread src/argon2.c src/core.c src/blake2/blake2b.c src/thread.c
src/encoding.c src/ref.c src/run.c -o argon2, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [argon2] Error 2
I referred to this stack overflow post and I think I am getting this error because I do not have 'cc' installed. What is 'cc' and am I correct about 'cc' needing to be installed? I am on windows by the way. I know this is a super specific question but maybe someone can help me.

Resources