gcc command line structure arguments - gcc-warning

I try to compile a c file with gcc version 6.3.0 20170516 (Raspbian 6.3.0-18+rpi1+deb9u1).
I run the compiler in the source file's folder, but I keep getting the 'file not found' error message for the last argument ('bcm2835').
gcc -o gpio -l rt /home/pi/bcm2835-1.15/src/bcm2835.c -l bcm2835
/usr/bin/ld: cannot find -lbcm2835
collect2: error: ld returned 1 exit status
AFAIK, The gcc does not specify the third argument, I have no idea what this 3rd argument is used for and where to find it.
If I omit that argument, I get several error lines, each for one of the internal commands, like:
undefined reference to bcm2835_init
I also wish to know what the rt stands for. I could not find it in the official gcc docs.
Thanks

After -l, there should be no space. So it should be -lrt (not -l rt) and it should be -lbcm2835 (not -l bcm2835).
You need to add a linker search path with -L (uppercase L) right before calling -lbcm2835.
The input file should usually be last (that's the argument ending with main.c). That would be
gcc -o main -lrt -lbcm2835 /home/pi/bcm2835-1.15/main.c
-l links a library to the program. -L sets the library search path such that -l will find the libraries specified.
See this page for details on -l and -L.

Related

LFS: GCC links wrong

I'm stuck on chapter 6.20 of the current LFS book. I get:
$ readelf -l a.out | grep Requesting
[Requesting program interpreter: /tools/lib64/ld-linux-x86-64.so.2]
instead of the desired:
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
The other tests lower down in the chapter all succeed.
This is my second time through the whole book being extremely careful and I got the same result last time around. Could there be some mistake?
For guidance it just said "The most likely reason is that something went wrong with the specs file adjustment.". That's a reference to 6.10 where it says "It is a good idea to visually inspect the specs file to verify the intended change was actually made" but there's no guidance as to what these intended changes are. But on this second go round I did check in /tools/lib/gcc/x86_64-pc-linux-gnu/7.2.0/specs that there was no mention of "tools" and the /usr paths were mentioned.
I seem to have solved it by making another specs file for the new compiler:
gcc -dumpspecs | sed -e 's#/tools##g' > `dirname $(gcc --print-libgcc-file-name)`/specs
After that, I redo the test:
echo 'int main(){}' > dummy.c
cc dummy.c -v -Wl,--verbose &> dummy.log
readelf -l a.out | grep ': /lib'
And all is well.

Error in Makefile calling sed with comment character

I'm trying to build lstrip, a Lua utility for compressing Lua source code. I'm trying to build for Lua 5.1.3 on OS X v10.10.3.
I have downloaded and extracted the Lua 5.1.3 source code and modified the Makefile for lstrip to point to this directory. However, when I run make, I get this output:
cc -I/usr/local/src/lua-5.1.3/src -I/usr/local/src/lua-5.1.3/src -O2 -Wall -Wextra -O2 -c -o lstrip.o lstrip.c
lstrip.c:33:14: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char* argv[])
^
1 warning generated.
sed '/void luaX_next/i#include "proxy.c"' /usr/local/src/lua-5.1.3/src/llex.c > llex.c
sed: 1: "/void luaX_next/i#inclu ...": command i expects \ followed by text
make: *** [llex.c] Error 1
This is what the relevant Makefile command looks like:
llex.c:
sed '/void luaX_next/i#include "proxy.c"' $(LUASRC)/$# > $#
I think that this is because the # in the sed command is being treated like an actual comment, but I'm not sure.
How can I fix the Makefile, or manually run the steps, to get lstrip built?
Full copy of the Makefile follows, in case it matters:
# makefile for lstrip
# change these to reflect your Lua installation (Lua 5.1!)
LUA= /usr/local/src/lua-5.1.3
LUAINC= $(LUA)/src
LUALIB= $(LUA)/src
LUASRC= $(LUA)/src
# no need to change anything below here
CFLAGS= $(INCS) $(WARN) -O2 $G
WARN= -O2 -Wall -Wextra
INCS= -I$(LUAINC) -I$(LUASRC)
LIBS= -L$(LUALIB) -llua -lm
MYNAME= lstrip
MYLIB= $(MYNAME)
T= $(MYNAME)
OBJS= $(MYNAME).o llex.o
TEST= test.lua
all: test
test: $T
$T $(TEST)
$T: $(OBJS)
$(CC) -o $# $(OBJS) $(LIBS)
llex.c:
sed '/void luaX_next/i#include "proxy.c"' $(LUASRC)/$# > $#
llex.o: proxy.c
clean:
-rm -f $(OBJS) core core.* a.out $(MYNAME)
# eof
Hand-build solution:
cp /usr/local/src/lua-5.1.3/src/llex.c .
Hand-edit llex.c to add the line #include "proxy.c" before the line starting with void luaX_next (line 446 for me).
Now run make, which will succeed.
You can find the answer in the sed manual, and it is in the Makefile lines
llex.c:
sed '/void luaX_next/i#include "proxy.c"' $(LUASRC)/$# > $#
Some variable expansion takes place here:
$(LUASRC) is expanded to the variable set above -> $(LUA)/src. Recurse as needed.
$# is replaced with the current target (llex.c)
So this recipe says:
In order to obtain the target llex.c (which will be processed later through other recipes), apply a stream editing command to the file $LUASRC/llex.c and write it to llex.c.
The stream editing command is: look for text "void luaX_next", before printing it, insert line "#include "proxy.c"".
Problem is, the command to do this is not "i" but "i\(newline)", which conflicts with a requirement of Makefiles that recipes must be on a single line.
I suspect that in order to fix your Makefile you need to use a different command than sed; awk can fit the bill although it's a bit more complex.
This line works in Mac OS X and in Linux:
sed '/void luaX_next/{h;s/.*/#include "proxy.c"/;p;g;}' $(LUASRC)/$# > $#

getting error y.tab.c:(.text+0x543): undefined reference to `pow'

I have written lex and yacc program for calculator. In yacc program I have included math.h for arithmetic operations like sin, cos, pow etc. for compilation i use below steps
lex calculator.
yacc -d calculator.y
cc -c lex.yy.c y.tab.c
cc lex.yy.c y.tab.c -o a.out
It gives me this error:
y.tab.c:undefined reference to 'pow'
Add -lm to the 2nd cc command-line:
cc lex.yy.c y.tab.c -o a.out -lm
The pow function is defined in the library named m (which is a shorthand for math), and this library must be linked (-l) to the executable so that the function is available at runtime.

C make file is really confusing me

I'm working on learning C from a family member who's very adept as far as I know.
I'm using MingW on windows 7 with a fresh install of windows and having some difficulties getting things to work properly. I've made a make file and am using Deitel C for programmers with an introduction to C11, and have typed up the examples from the book in chapter three. I'm fairly certain I've managed to get that portion correct, and the makefile I'm using seems to be correct but it gives me a strange error that I don't understand.
C -o ex01 -O3 -Wall -Werrors -static -pedantic-errors -g main.o
make: C: Command not found
make: * [ex01] Error 127
this is the exact error I keep getting, I'm not sure if there's something wrong with the makefile or if it's something wrong with settings...
RM=rm
CC=gcc
LINK=$CC
CFLAGS = -O3 -Wall -Werrors -static -pedantic-errors -g
all: main.o ex01
clean:
(Tab)$(RM) -f main.o ex01.exe
main.o: main.c
(Tab)$(CC) -o main.o $(CFLAGS) -c main.c
ex01: main.o
(Tab)$(LINK) -o ex01 $(CFLAGS) main.o
this is almost exactly the makefile I'm using, asside from the (Tab) in place of actual tabs. I hope this is enough information to get some help, I suspect it's something wrong with my settings, I've had to set up paths to my library and gcc location. I'm just not sure where else to set up a path that could rectify this error.
#Keltar's comment nailed it exactly: LINK=$CC should be LINK=$(CC).
In make's syntax, $() is the proper way to deference a variable. The line LINK=$(CC) means set the variable LINK to whatever the CC variable is set to. After that instruction, their values will be the same.

How to write a Makefile rule to download a file only if it is missing?

I'm trying to write a Makefile which should download some sources if and only if they are missing.
Something like:
hello: hello.c
gcc -o hello hello.c
hello.c:
wget -O hello.c http://example.org/hello.c
But of course this causes hello.c to be downloaded every time make command is run. I would like hello.c to be downloaded by this Makefile only if it is missing. Is this possible with GNU make and how to do this if it is?
My guess is that wget doesn't update the timestamp on hello.c, but retains the remote timestamp. This causes make to believe that hello.c is old and attempts to download it again. Try
hello.c:
wget ...
touch $#
EDIT: The -N option to wget will prevent wget from downloading anything unless the remote file is newer (but it'll still check the timestamp of the remote file, of course.)
Since the Makefile should be working as you want, you need to check a few unlikely cases:
1) Check that you don't have any .PHONY rules mentioning the source file.
2) Check that the source target name matches the file path you are downloading.
You could also try running make -d to see why make thinks it needs to 're-build' the source file.
The Makefile you wrote downloads hello.c only if it's missing. Perhaps you are doing something else wrong? See for example:
hello: hello.c
gcc -o hello hello.c
hello.c:
echo 'int main() {}' > hello.c
And:
% make
echo 'int main() {}' > hello.c
gcc -o hello hello.c
% rm hello
% make
gcc -o hello hello.c
% rm hello*
% make
echo 'int main() {}' > hello.c
gcc -o hello hello.c
(the echo command was not executed the second time)
If the prerequisite for hello.c has changed or is empty and Make continues to download the file when it exists, then one option to prevent Make from re-downloading the file is to use a flag in the body of the target to detect if the file exists:
hello.c:
test -f $# || wget -O hello.c http://example.org/hello.c
The test command will return true if the hello.c file exists, otherwise it will return false and the wget command will run.