How does make use variables when expanding and compiling before linking? - linux

Info:
Linux watvde0453 3.10.0-1062.1.1.el7.x86_64 #1 SMP Fri Sep 13 22:55:44 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
GNU Make 4.2.1
Built for x86_64-pc-linux-gnu
Hi
I have been playing around with a make file to allow builds of shared, static libraries and an exe (and also to get some sort of understanding about how it works) and came across some behavior I do not understand.
I was trying to separate out the flags used for lib / exe into separate variables, but when using a line to compile and link all in one it looks like only the CFLAGS variable is being included for the compile step.
When using the following makefile:
LIBCFLAGS := -fPIC
CFLAGS := -O3 -g -Wall -Werror
CC := gcc
SRCS=hashfunction.c hashtable.c hashtablelinkedlist.c
OBJS=hashfunction.o hashtable.o hashtablelinkedlist.o
LIBSO = lib$(NAME).so
libso: $(OBJS)
$(CC) $(LIBCFLAGS) $(CFLAGS) -shared -o $(LIBSO) $(OBJS)
I get the following output when running the make command for libso:
$ make libso
gcc -O3 -g -Wall -Werror -c -o hashfunction.o hashfunction.c
gcc -O3 -g -Wall -Werror -c -o hashtable.o hashtable.c
gcc -O3 -g -Wall -Werror -c -o hashtablelinkedlist.o hashtablelinkedlist.c
gcc -fPIC -O3 -g -Wall -Werror -shared -o lib.so hashfunction.o hashtable.o hashtablelinkedlist.o
/tools/oss/packages/x86_64-centos7/binutils/default/bin/ld: hashtable.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
hashtable.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [Makefile:12: libso] Error 1
I can get it all working by sticking all the flags in CFLAGS, but I was wondering if anyone could explain what make is doing underneath ?
It looks like the $(LIBCFLAGS) is being ignored for the implicit compile lines, but $(CFLAGS) is not. Is CFALGS used implicitly by make for all compilations ?

You can see all the built-in rules make knows about by running make -p -f/dev/null. There you'll see:
%.o: %.c
# recipe to execute (built-in):
$(COMPILE.c) $(OUTPUT_OPTION) $<
which is the built-in rule make uses to create a .o file from a .c file. Looking elsewhere in the output you'll see:
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
so these are the variables make uses to compile .c files into .o files.

Related

make "cannot find libraries"

I am trying to compile til parameter estimation tool PEST (http://www.pesthomepage.org/) for linux.
According to the PEST-manual I did:
make -f pest.mak all
but I get following error message:
gfortran -c -O3 -static pestdata.for
gfortran -c -O3 -static pest.for
gfortran -c -O3 -static pestsub2.for
gfortran -c -O3 -static writall.for
gfortran -c -O3 -static pardef.for
gfortran -c -O3 -static readpest.for
gfortran -c -O3 -static runpest.for
gfortran -static -o pest \
pest.o pestsub1.o pestsub2.o dercalc.o modrun.o writall.o \
linpos.o lapack1.o writsig.o common.o \
pgetcl.o pestwait.o writint.o pardef.o\
drealrd.o space.o optwt.o cgsolve.o compress.o \
readpest.o runpest.o lsqr.o orthog.o ms_stubs.o pestdata.o
/bin/ld: cannot find -lgfortran
/bin/ld: cannot find -lm
/bin/ld: cannot find -lquadmath
/bin/ld: cannot find -lm
/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
make: *** [pest] Error 1
After goggling I have tried different things, but without results. Can somebody please help
me?
Regards Tanja
Solutions to a problem shouldn't be hidden in comments, which is why I write this answer.
The make output shows that the Makefile tries to link statically.
That requires the libraries that can be linked statically, notably libm.a, libgfortran.a, libquadmath.a and libc.a
A possible solution is to remove the -static flag in the final step.
The resulting executable will of course need the shared objects (libm.so and friends) in order to be able to run. In most situations that's not a problem though.

c++ makfile error: makefile:5: *** missing separator. Stop

I'm trying to run my c++ (written in clion) program in linux. When I try to compile it in the terminal using "make" command, I get this error:
"makefile:5: *** missing separator. Stop."
I already checked that there tabs and not 4 spaces in my makefile.
Anyone has an idea?
Thanks!
This is my makefile:
CFLAGS := -c -Wall -Weffc++ -g -std=c++11 -Iinclude
LDFLAGS := -lboost_system
all: StompBookClubClient
g++ -pthread -o bin/StompBookClubClient bin/ConnectionHandler.o bin/Book.o bin/keyboardInputSend.o bin/socketReader.o bin/User.o $(LDFLAGS)
StompBookClubClient: bin/StompBookClubClient bin/ConnectionHandler.o bin/Book.o bin/keyboardInputSend.o bin/socketReader.o bin/User.o
bin/Book.o: src/Stomp/Book.cpp
g++ -pthread $(CFLAGS) -o bin/Book.o src/Book.cpp
bin/ConnectionHandler.o: src/Stomp/ConnectionHandler.cpp
g++ -pthread $(CFLAGS) -o bin/ConnectionHandler.o src/ConnectionHandler.cpp
bin/keyboardInputSend.o: src/Stomp/keyboardInputSend.cpp
g++ -pthread $(CFLAGS) -o bin/keyboardInputSend.o src/keyboardInputSend.cpp
bin/socketReader.o: src/Stomp/socketReader.cpp
g++ -pthread $(CFLAGS) -o bin/socketReader.o src/socketReader.cpp
bin/StompBookClubClient.o: src/Stomp/StompBookClubClient.cpp
g++ -pthread $(CFLAGS) -o bin/StompBookClubClient.o src/StompBookClubClient.cpp
bin/User.o: src/Stomp/User.cpp
g++ -pthread $(CFLAGS) -o bin/User.o src/User.cpp
.PHONY: clean
clean:
rm -f bin/*
I already checked that there tabs and not 4 spaces in my makefile.
Check it a bit harder. The Makefile you pasted here has 4 spaces on line 5 and produces exactly the error you are seeing. If I replace them by a tab, the next error occurs on line 10, and so on.
This is not an answer but I do not have sufficient points to comment and hence answering.
Apart from 'tab' issue ,you get a similar error if ':'(colon) is missed after the rule name.
Ex makefile:
helloworld.o
g++ helloworld.cc -o helloworld.o;
Error:
Makefile:1: *** missing separator. Stop.
Solution:
Colon after helloworld.o like below
helloworld.o:
g++ helloworld.cc -o helloworld.o;

Makefile for Linux and Mac with address sanitizer

I am trying to write a makefile that I can use on linux and mac that builds with an address sanitizer. This works on my vagrant instance:
CC = gcc
ASAN_FLAGS = -fsanitize=address -fno-omit-frame-pointer -Wno-format-security
ASAN_LIBS = -static-libasan
CFLAGS := -Wall -Werror --std=gnu99 -g3
LDFLAGS += -lpthread
all: hello
hello: tiny_queue.o hello.o
$(CC) -o $# $(CFLAGS) $(ASAN_FLAGS) $(CURL_CFLAGS) $^ $(LDFLAGS) $(CURL_LIBS) $(ASAN_LIBS)
This works on ubuntu/trusty64 but fails on my mac with
$ make
gcc -Wall -Werror --std=gnu99 -g3 -I/opt/X11/include -c -o hello.o hello.c
gcc -o hello -Wall -Werror --std=gnu99 -g3 -fsanitize=address -fno-omit-frame-pointer -Wno-format-security tiny_queue.o hello.o -lpthread -static-libasan
clang: error: unknown argument: '-static-libasan'
make: *** [hello] Error 1
Does anyone know how to write a compatible makefile for the mac and linux?
p.s. I'm very new to C, sorry if this question is super basic.
CC = gcc
ASAN_FLAGS = -fsanitize=address -fno-omit-frame-pointer -Wno-format-security
ASAN_LIBS = -static-libasan
CFLAGS := -Wall -Werror --std=gnu99 -g3
LDFLAGS += -lpthread
all: hello
hello: tiny_queue.o hello.o
$(CC) -o $# $(CFLAGS) $(ASAN_FLAGS) $(CURL_CFLAGS) $^ $(LDFLAGS) $(CURL_LIBS) $(ASAN_LIBS)
You should not specify an Asan library (or a UBsan library, for that matter). Since you are using the compiler driver to drive link, just use -fsanitize=address (this is the recommended way of doing it). Do not add -static-libasan. The compiler driver will add the proper libraries for you.

How to use static library with gcc under linux

I'm trying to use a static library 'mylib.a' created with 'ar'. The examples I have seen use -L and -l to name the library. My attempts at using these fail. However, if I simply put the library file name on the linker command it works. (I'm overlooking something obvious?) Using gcc 4.8.4 Ubuntu 14.04 (see comment in code.) The f?.c and mymain.c are trivially simple. Thanks!
#!/bin/bash
# cc -v shows...
# gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.1)
cc -c -Wall -Wstrict-prototypes -o fa.o fa.c
cc -c -Wall -Wstrict-prototypes -o fb.o fb.c
cc -c -Wall -Wstrict-prototypes -o fc.o fc.c
cc -c -Wall -Wstrict-prototypes -o mymain.o mymain.c
ar -rcs mylib.a fa.o fb.o fc.o
# THIS WORKS
#cc -o mypgm mymain.o mylib.a
# THESE FAIL WITH ERROR '/usr/bin/ld: cannot find -lmylib'
cc -o mypgm mymain.o -L. -lmylib
# OR
cc -o mypgm mymain.o -static -L. -lmylib
###
As noted in a comment…
When you use -lname, the linker looks for libname.a or libname.so in one of the directories that it has (already) been told to search for libraries.
You can either rename your library to libmine.a and then -L. -lmine will work, or rename it to libmylib.a and then -lmylib will work (but the repeated lib looks a bit clumsy, or neophytic).

How to build a user level program in parallel with building Linux kernel

I have a set of kernel source files in ~linux/fs/wrapfs/. Besides, there is also a source file present for a user level program. I want to build that user level program while the kernel gets compiled by running "make" in ~/linux folder i.e in the top level kernel directory.
So, I tried adding following in ~/linux/fs/wrapfs/Makefile:
EXTRA_CFLAGS += prog
prog:
gcc -Wall -Werror prog.c -o prog -lssl -lcrypto
But, then everytime I do "make" in ~/linux, I get following error:
gcc: prog: No such file or directory
I then tried changing it to following but nothing helped:
EXTRA_CFLAGS += prog
all:
gcc -Wall -Werror prog.c -o prog -lssl -lcrypto
EXTRA_CFLAGS += prog
prog:
gcc -Wall -Werror ./fs/wrapfs/prog.c -o ./fs/wrapfs/prog -lssl -lcrypto
Please suggest how can this be solved.
You should not be trying to pass prog in CFLAGS, extra or otherwise.

Resources