* Header file dependency, rpath, soname and more with GCC
@ 2023-03-14 18:45 ljh
2023-03-14 18:53 ` ljh
0 siblings, 1 reply; 3+ messages in thread
From: ljh @ 2023-03-14 18:45 UTC (permalink / raw)
To: gcc-help
[-- Attachment #1: Type: text/plain, Size: 5205 bytes --]
Hi community,
Currently, I can handle these tasks in C and C++ programming with GCC
on Linux:
1. Header file dependency;
2. rpath (for build and install);
3. soname for so versioning;
4. Debug or Release mode;
5. out-of-source build;
I want to know if there are some other tasks which are missing from the
above list and I need to handle them too. Many thanks!
The above tasks can be handled in my CMake, Autotools, or manual Makefile.
Below is my Makefile.
---
$ tree --charset C
.
|-- build
| |-- foo
| | |-- foo
| | |-- foo.d
| | |-- foo.o
| | |-- libfoo.so -> libfoo.so.1.2.3
| | |-- libfoo.so.1 -> libfoo.so.1.2.3
| | `-- libfoo.so.1.2.3
| `-- main
| |-- main
| |-- main.d
| `-- main.o
|-- foo
| |-- foo.c
| |-- foo.h
| `-- Makefile
|-- main
| |-- main.c
| `-- Makefile
`-- Makefile
$
---
# Makefile for top dir
# $(call makever,1.2.3)
# major.minor.patch
# libtool manual: -version-number
define makever
@ $(MAKE) -C $@ soname=lib$@.so.$(word 1,$(subst ., ,$(1)))
@ cp $(OBJDIR)/$@/$@ $(OBJDIR)/$@/lib$@.so.$(1)
@ cd $(OBJDIR)/$@ ; \
ln -f -s lib$@.so.$(1) lib$@.so.$(word 1,$(subst ., ,$(1))) ; \
cd - >/dev/null 2>&1 ;
@ cd $(OBJDIR)/$@ ; \
ln -f -s lib$@.so.$(1) lib$@.so ; \
cd - >/dev/null 2>&1 ;
endef
# make # BUILD_DIR=build
ifdef BUILD_DIR
export OBJDIR = $(abspath $(BUILD_DIR))
else
export OBJDIR = $(abspath build)
endif
SUBDIRS = main foo
all : $(SUBDIRS)
install : $(SUBDIRS)
$(SUBDIRS) : | $(OBJDIR)
$(OBJDIR) : ; @ mkdir $@
main : foo
main : ; @ $(MAKE) -C $@
foo : ; $(call makever,1.2.3)
# make DESTDIR=~/foo install
# Alexandre Duret-Lutz's Autotools Tutorial (without animations):
# "is ready to be uncompressed in / on many hosts"
install :
install -d $(DESTDIR)/usr/local/bin
install -d $(DESTDIR)/usr/local/lib
install -m 0755 $(OBJDIR)/main/main $(DESTDIR)/usr/local/bin
cp -P $(OBJDIR)/foo/*.so* $(DESTDIR)/usr/local/lib
clean :
@ for dir in $(SUBDIRS); do \
$(MAKE) -C $$dir $@; \
done
-rm -fr $(OBJDIR)
.PHONY : $(SUBDIRS) all install clean
---
# Makefile for subdir
# build shared library with -fPIC, -shared
CFLAGS = # -g -O3 -fPIC # CXXFLAGS for .cpp
CPPFLAGS = -MMD -MP -I../foo
LDFLAGS = -L$(OBJDIR)/foo # -shared
LDLIBS = -lfoo
#CC = $(CXX) # link with CXX for .cpp
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../foo'
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../lib'
#LDFLAGS += -Wl,-soname,$(soname)
# make # NDEBUG=1
ifdef NDEBUG
CFLAGS += -O3 # .cpp
CPPFLAGS += -DNDEBUG
else
CFLAGS += -g # .cpp
LDFLAGS += -fsanitize=address
endif
SUBDIR = $(OBJDIR)/$(lastword $(subst /, ,$(CURDIR)))
all : $(SUBDIR)/main # $(SUBDIR)/foo
# https://make.mad-scientist.net/papers/how-not-to-use-vpath/
$(SUBDIR)/main : $(addprefix $(SUBDIR)/,$(patsubst %.c,%.o,$(wildcard *.c))) # .cpp
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(SUBDIR)/%.o : %.c | $(SUBDIR) # .cpp
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
$(SUBDIR) : ; @ mkdir $@
-include $(SUBDIR)/*.d
clean : ; -rm -fr $(SUBDIR)
.PHONY : all clean
---
# Makefile for subdir
# build shared library with -fPIC, -shared
CFLAGS = -fPIC # -g -O3 # CXXFLAGS for .cpp
CPPFLAGS = -MMD -MP # -I../bar
LDFLAGS = -shared # -L$(OBJDIR)/bar
LDLIBS = # -lbar
#CC = $(CXX) # link with CXX for .cpp
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../bar'
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../lib'
LDFLAGS += -Wl,-soname,$(soname)
# make # NDEBUG=1
ifdef NDEBUG
CFLAGS += -O3 # .cpp
CPPFLAGS += -DNDEBUG
else
CFLAGS += -g # .cpp
LDFLAGS += -fsanitize=address
endif
SUBDIR = $(OBJDIR)/$(lastword $(subst /, ,$(CURDIR)))
all : $(SUBDIR)/foo # $(SUBDIR)/bar
# https://make.mad-scientist.net/papers/how-not-to-use-vpath/
$(SUBDIR)/foo : $(addprefix $(SUBDIR)/,$(patsubst %.c,%.o,$(wildcard *.c))) # .cpp
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(SUBDIR)/%.o : %.c | $(SUBDIR) # .cpp
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
$(SUBDIR) : ; @ mkdir $@
-include $(SUBDIR)/*.d
clean : ; -rm -fr $(SUBDIR)
.PHONY : all clean
---
#include <stdio.h>
#include <signal.h>
#include <assert.h>
#ifndef NDEBUG
#include <sanitizer/lsan_interface.h>
#endif
#include "foo.h"
void handlerCont(int signum) {
printf("SIGCONT %d\n", signum);
#ifndef NDEBUG
__lsan_do_recoverable_leak_check();
#endif
}
int main() {
signal(SIGCONT, handlerCont); // kill -CONT 123 # pid
printf("main\n");
foo();
int a[1024];
int n = 10240;
a[n] = 1; // asan
assert(0); // -DNDEBUG
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re:Header file dependency, rpath, soname and more with GCC
2023-03-14 18:45 Header file dependency, rpath, soname and more with GCC ljh
@ 2023-03-14 18:53 ` ljh
2023-03-15 6:33 ` Header " LIU Hao
0 siblings, 1 reply; 3+ messages in thread
From: ljh @ 2023-03-14 18:53 UTC (permalink / raw)
To: gcc-help
(Fixed the web mail format. Sorry!)
Hi community,
Currently, I can handle these tasks in C and C++ programming with GCC
on Linux:
1. Header file dependency;
2. rpath (for build and install);
3. soname for so versioning;
4. Debug or Release mode;
5. out-of-source build;
I want to know if there are some other tasks which are missing from the
above list and I need to handle them too. Many thanks!
The above tasks can be handled in my CMake, Autotools, or manual Makefile.
Below is my Makefile.
---
# Makefile for top dir
# $(call makever,1.2.3)
# major.minor.patch
# libtool manual: -version-number
define makever
@ $(MAKE) -C $@ soname=lib$@.so.$(word 1,$(subst ., ,$(1)))
@ cp $(OBJDIR)/$@/$@ $(OBJDIR)/$@/lib$@.so.$(1)
@ cd $(OBJDIR)/$@ ; \
ln -f -s lib$@.so.$(1) lib$@.so.$(word 1,$(subst ., ,$(1))) ; \
cd - >/dev/null 2>&1 ;
@ cd $(OBJDIR)/$@ ; \
ln -f -s lib$@.so.$(1) lib$@.so ; \
cd - >/dev/null 2>&1 ;
endef
# make # BUILD_DIR=build
ifdef BUILD_DIR
export OBJDIR = $(abspath $(BUILD_DIR))
else
export OBJDIR = $(abspath build)
endif
SUBDIRS = main foo
all : $(SUBDIRS)
install : $(SUBDIRS)
$(SUBDIRS) : | $(OBJDIR)
$(OBJDIR) : ; @ mkdir $@
main : foo
main : ; @ $(MAKE) -C $@
foo : ; $(call makever,1.2.3)
# make DESTDIR=~/foo install
# Alexandre Duret-Lutz's Autotools Tutorial (without animations):
# "is ready to be uncompressed in / on many hosts"
install :
install -d $(DESTDIR)/usr/local/bin
install -d $(DESTDIR)/usr/local/lib
install -m 0755 $(OBJDIR)/main/main $(DESTDIR)/usr/local/bin
cp -P $(OBJDIR)/foo/*.so* $(DESTDIR)/usr/local/lib
clean :
@ for dir in $(SUBDIRS); do \
$(MAKE) -C $$dir $@; \
done
-rm -fr $(OBJDIR)
.PHONY : $(SUBDIRS) all install clean
---
# Makefile for subdir
# build shared library with -fPIC, -shared
CFLAGS = # -g -O3 -fPIC # CXXFLAGS for .cpp
CPPFLAGS = -MMD -MP -I../foo
LDFLAGS = -L$(OBJDIR)/foo # -shared
LDLIBS = -lfoo
#CC = $(CXX) # link with CXX for .cpp
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../foo'
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../lib'
#LDFLAGS += -Wl,-soname,$(soname)
# make # NDEBUG=1
ifdef NDEBUG
CFLAGS += -O3 # .cpp
CPPFLAGS += -DNDEBUG
else
CFLAGS += -g # .cpp
LDFLAGS += -fsanitize=address
endif
SUBDIR = $(OBJDIR)/$(lastword $(subst /, ,$(CURDIR)))
all : $(SUBDIR)/main # $(SUBDIR)/foo
# https://make.mad-scientist.net/papers/how-not-to-use-vpath/
$(SUBDIR)/main : $(addprefix $(SUBDIR)/,$(patsubst %.c,%.o,$(wildcard *.c))) # .cpp
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(SUBDIR)/%.o : %.c | $(SUBDIR) # .cpp
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
$(SUBDIR) : ; @ mkdir $@
-include $(SUBDIR)/*.d
clean : ; -rm -fr $(SUBDIR)
.PHONY : all clean
---
# Makefile for subdir
# build shared library with -fPIC, -shared
CFLAGS = -fPIC # -g -O3 # CXXFLAGS for .cpp
CPPFLAGS = -MMD -MP # -I../bar
LDFLAGS = -shared # -L$(OBJDIR)/bar
LDLIBS = # -lbar
#CC = $(CXX) # link with CXX for .cpp
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../bar'
LDFLAGS += -Wl,-rpath,'$$ORIGIN/../lib'
LDFLAGS += -Wl,-soname,$(soname)
# make # NDEBUG=1
ifdef NDEBUG
CFLAGS += -O3 # .cpp
CPPFLAGS += -DNDEBUG
else
CFLAGS += -g # .cpp
LDFLAGS += -fsanitize=address
endif
SUBDIR = $(OBJDIR)/$(lastword $(subst /, ,$(CURDIR)))
all : $(SUBDIR)/foo # $(SUBDIR)/bar
# https://make.mad-scientist.net/papers/how-not-to-use-vpath/
$(SUBDIR)/foo : $(addprefix $(SUBDIR)/,$(patsubst %.c,%.o,$(wildcard *.c))) # .cpp
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(SUBDIR)/%.o : %.c | $(SUBDIR) # .cpp
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
$(SUBDIR) : ; @ mkdir $@
-include $(SUBDIR)/*.d
clean : ; -rm -fr $(SUBDIR)
.PHONY : all clean
---
#include <stdio.h>
#include
#include <assert.h>
#ifndef NDEBUG
#include <sanitizer lsan_interface.h="">
#endif
#include "foo.h"
void handlerCont(int signum) {
printf("SIGCONT %d\n", signum);
#ifndef NDEBUG
__lsan_do_recoverable_leak_check();
#endif
}
int main() {
signal(SIGCONT, handlerCont); // kill -CONT 123 # pid
printf("main\n");
foo();
int a[1024];
int n = 10240;
a[n] = 1; // asan
assert(0); // -DNDEBUG
}</sanitizer></assert.h></stdio.h>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Header file dependency, rpath, soname and more with GCC
2023-03-14 18:53 ` ljh
@ 2023-03-15 6:33 ` LIU Hao
0 siblings, 0 replies; 3+ messages in thread
From: LIU Hao @ 2023-03-15 6:33 UTC (permalink / raw)
To: ljh, gcc-help
[-- Attachment #1.1: Type: text/plain, Size: 869 bytes --]
在 2023/3/15 02:53, ljh via Gcc-help 写道:
> (Fixed the web mail format. Sorry!)
>
> Hi community,
>
> Currently, I can handle these tasks in C and C++ programming with GCC
> on Linux:
>
> 1. Header file dependency;
> 2. rpath (for build and install);
> 3. soname for so versioning;
> 4. Debug or Release mode;
> 5. out-of-source build;
>
> I want to know if there are some other tasks which are missing from the
> above list and I need to handle them too. Many thanks!
>
> The above tasks can be handled in my CMake, Autotools, or manual Makefile.
> Below is my Makefile.
You should start to use libtool with autotools (and stop writing Makefiles by hand), which does all
these pieces of dirty work for you, and makes your program (likely to) run on platforms that you
probably have never heard of.
--
Best regards,
LIU Hao
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-03-15 6:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14 18:45 Header file dependency, rpath, soname and more with GCC ljh
2023-03-14 18:53 ` ljh
2023-03-15 6:33 ` Header " LIU Hao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).