How to compile C file through Terminal - c

I'm trying to compile a C file that I have saved on my computer and I'm trying to compile the file with Terminal on my Mac OS X El Capitan by typing out gcc - g -wall -werror -o getit.out -std=c11 getit.c, but I keep getting this error stating:
Sandra$ gcc -g -wall -werror -o getit.out -std=c11 getit.c
clang: error: unknown argument: '-wall'
clang: error: unknown argument: '-werror'
clang: error: -E or -x required when input is from standard input
clang: error: no such file or directory: 'g'
I have downloaded Xcode and installed it, but I'm not sure why I'm getting this error. If anyone could provide insight that would be greatly appreciated. Thank you!
EDIT:
So as stated by #md xf I tried
gcc - g -Wall -Werror -o getit.out -std=c11 getit.c
but I still got an error stating
clang: error: -E or -x required when input is from standard input
clang: error: no such file or directory: 'g'
**Forgot to list all of my errors in my original post

As far as I know, the W in -Wall and -Werror need to be capitalized. A capital flag and a lowercase flag will have two completely different functions. So you would rewrite it as gcc -Wall -Werror -std=c11 -o getit.out getit.c.
In response to your edit you could also try:
writing getit.c as your first option
making sure there is no space between - and g (-g)
removing the -std=c11 if it's not absolutely necessary
It seems like you are typing something different than what's shown here. The error messages make it seem like there's a space between - and g.

clang: error: unknown argument: '-wall'
clang: error: unknown argument: '-werror'
The W in -Wall and -Werror need to be capitalized. That explains the above errors showed by gcc.
clang: error: -E or -x required when input is from standard input
clang: error: no such file or directory: 'g'
This error is due to space character between g flag option and hypen(-). Remove the space and this error will run away.
The command to compile your program should be like this:
gcc -Wall -Werror -std=c11 -o getit.out getit.c -g.

please use command as below to compile the C file
gcc - g -Wall -Werror -o getit.out -std=c11 getit.c

Related

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

how to run a C program with a test file?

I compile the program with gcc
gcc -Wall -ansi -pedantic -o program.c
what is the command to run it with my test file?
./program <test1.txt> ???
Your gcc is wrong and at present errors on building.
$ gcc -Wall -ansi -pedantic -o program.c
Will show this error:
gcc: fatal error: no input files
compilation terminated.
Try:
gcc -Wall -ansi -pedantic -o program program.c
This will compile your program into an exec called program, which you can then run by;
./program
Then maybe try running you're command passing in the test1.txt
./program test1.txt
Please see comments in your OP as people have already offered good advise.

gcc no such file or directory error

I am trying to compile a C program and am required to use these flags. I was hoping you could tell me why I am getting these errors.
Command:
gcc -ansi –Wall –pedantic stack.c
Output:
gcc: –Wall: No such file or directory
gcc: –pedantic: No such file or directory
It's
gcc -ansi -Wall -pedantic
You're using one of those dashes: Dash (specifically, you are using en-dash U+2013). You need to use minus sign - instead

Running ./configure with -Wall leads to libraries not being found

I'd like to use -Wall and -Werror as flags for gcc in an autotools project, but I don't want to put them in my configure.ac.
As such, I tried using ./configure CFLAGS='-Wall -Werror', only to get an error from one of my AC_SEARCH_LIBS macro calls:
AC_SEARCH_LIBS([pow], [m], , AC_MSG_ERROR([Could not find standard math library.]))
Resulting error when running configure with the CFLAGS added:
configure: error: Could not find standard math library.
What am I doing wrong here? Configuration works fine without the CFLAGS variable set.
As you now know, elevating the compile warnings to errors confuses ./configure.
What you can do is pass custom CFLAGS at make time:
$ ./configure
$ make CFLAGS='-O2 -g -Wall -Wextra -Werror'
The other option is William Pursell's approach: add an option to ./configure to turn on -Werror if supported:
(configure.ac)
AC_ARG_ENABLE([werror],
[AS_HELP_STRING([--enable-werror], [Use -Werror #<:#no#:>#])],
[:],
[enable_werror=no])
AM_CONDITIONAL([ENABLE_WERROR], [test "$enable_werror" = yes])
(Makefile.am)
if ENABLE_WERROR
AM_CFLAGS += -Werror
endif

CS107 Assignment file couldn't compile, missing expat.h and thread_107.h files

I was auditing cs107 at stanford online
The problem I ran into is with assignment 6, when I type "make" in terminal, the error message pops up. Basically, I miss two header files, which I guess can be got from the pre-compiled .lib file. But somehow it just doesn't work.
Here's part of the original make file:
CFLAGS = -D_REENTRANT -g -Wall -D__ostype_is_$(OSTYPE)__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function $(DFLAG)
LDFLAGS = -L/usr/class/cs107/assignments/assn-6-rss-news-search-lib/$(OSTYPE) -L/usr/class/cs107/lib -lexpat -lrssnews $(PLATFORM_LIBS) $(THREAD_LIBS)
PFLAGS= -linker=/usr/pubsw/bin/ld -best-effort -threads=yes -max-threads=1000
Edit:
When I said "This is supposed to compile even without threading implementation", I meant that it should compile without FURTHER threading implementation by students.
So here's the error message with thread:
gcc -D_REENTRANT -g -Wall -D__ostype_is_linux__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function -c -o rss-news-search.o rss-news-search.c
rss-news-search.c: In function ‘main’:
rss-news-search.c:109:3: warning: implicit declaration of function ‘InitThreadPackage’ [-Wimplicit-function-declaration]
gcc rss-news-search.o -D_REENTRANT -g -Wall -D__ostype_is_linux__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function -L/home/h/cs107/assn-6-rss-news-search-lib/linux -L/usr/class/cs107/lib -L. -lexpat -lrssnews -lnsl -lpthread -lthread_107_linux -o rss-news-search
/usr/bin/ld: cannot find -lthread_107_linux
collect2: ld returned 1 exit status
make: *** [rss-news-search] Error 1
here's the error message without $(THREAD_LIBS):
gcc -D_REENTRANT -g -Wall -D__ostype_is_linux__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function -c -o rss-news-search.o rss-news-search.c
rss-news-search.c: In function ‘main’:
rss-news-search.c:109:3: warning: implicit declaration of function ‘InitThreadPackage’ [-Wimplicit-function-declaration]
gcc rss-news-search.o -D_REENTRANT -g -Wall -D__ostype_is_linux__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function -L/home/h/cs107/assn-6-rss-news-search-lib/linux -L/usr/class/cs107/lib -L. -lexpat -lrssnews -lnsl -lpthread -o rss-news-search
rss-news-search.o: In function `main':
/home/h/cs107/assn-6-rss-news-search/rss-news-search.c:109: undefined reference to `InitThreadPackage'
collect2: ld returned 1 exit status
make: *** [rss-news-search] Error 1
In the later case, if I comment out "InitThreadPackage", it compiles just fine.
This is the procedure to compile your project:
Create a file assn-6-rss-news-search/thread_107.h, and put this inside:
/* Empty header file */
Copy the library librssnews.a from assn-6-rss-news-search-lib/linux/ to assn-6-rss-news-search/
Modify the file rss-news-search.c by commenting the call to the function : InitThreadPackage on line 109:
//InitThreadPackage(false);
Modify the Makefile to include the path to the current directory (to be able to link to the library you've copied earlier librssnews.a):
The line 27 should look like this:
LDFLAGS = -L/usr/class/cs107/assignments/assn-6-rss-news-search-lib/$(OSTYPE) -L/usr/class/cs107/lib -L. -lexpat -lrssnews $(PLATFORM_LIBS) $(THREAD_LIBS)
Then:
make clean
make
EDIT :
When you got this error cannot find lthread_107_linux, Edit your Makefile to remove this $(THREAD_LIBS) on line 27:
LDFLAGS = -L/usr/class/cs107/assignments/assn-6-rss-news-search-lib/$(OSTYPE) -L/usr/class/cs107/lib -L. -lexpat -lrssnews $(PLATFORM_LIBS)
The class-specific header files, like thread_107.h are found in /usr/class/cs107/include/ on whatever machine the instructor is expecting the students to use. If you're not using that machine, you'll have to copy those include files or make your own.
The expat.h file is from an open source library. You'll need to install the appropriate package on the system you're compiling on. On Ubuntu, that's sudo apt-get install libexpat1-dev, but the package name should be similar on other distributions.

Resources