Clang targets: thread-local storage is not supported - multithreading

There are several questions about compiler options. Now I use the following:
-target i386-windows-gnu -mno-sse -c -O3
-target x86_64-windows-gnu -mcx16 -c -O3
-target i386-linux-gnu -mno-sse -c -O3
-target x86_64-linux-gnu -mcx16 -c -O3
-target i386-darwin-gnu -mno-sse -fomit-frame-pointer -c -O3
-target x86_64-macos-gnu -fomit-frame-pointer -c -O3
-target armv7-none-linux-androideabi -mfpu=neon -mfloat-abi=hard -mthumb -fPIC -c -O3
-target aarch64-linux-android -c -O3
-target armv7m-none-ios-gnueabi -mfpu=neon -mfloat-abi=hard -mthumb -c -O3
-target arm64-darwin-gnu -fno-stack-protector -c -O3
There are no complaints only on Linux/Android. For other platforms it gives a warning (https://godbolt.org/z/YhZ5uc):
clang-9: warning: argument unused during compilation: '--gcc-toolchain=/opt/compiler-explorer/gcc-9.2.0' [-Wunused-command-line-argument]
Compiler returned: 0
ARM32 platforms do not support the preserve_most attribute (https://godbolt.org/z/SQRJB2):
<source>:2:21: warning: 'preserve_most' calling convention is not supported for this target [-Wignored-attributes]
void __attribute__((preserve_most)) proc_most(int* x);
But the biggest problem is that Mac/iOS does not support TLS variables (https://godbolt.org/z/6bqjby)!
__thread int x;
int test()
{
return x;
}
<source>:2:1: error: thread-local storage is not supported for the current target

Related

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

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.

How invoke dpkg-buildflags for manual compilation?

On Debian stretch when trying to manually compile e.g. libpopt (I'm not intending to create debian packages), after I did
export DEB_BUILD_OPTIONS=hardening=+all
I've trouble passing dpkg-buildflags to ./configure:
./configure $(dpkg-buildflags --export=cmdline)
configure: error: unrecognized option: -O2
Try `./configure --help' for more information.
If I do:
dpkg-buildflags --export=cmdline
I get:
CFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong -Wformat -Werror=format-security" CPPFLAGS="-Wdate-time -D_FORTIFY_SOURCE=2" CXXFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong -Wformat -Werror=format-security" FCFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong" FFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong" GCJFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong" LDFLAGS="-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now" OBJCFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong -Wformat -Werror=format-security" OBJCXXFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong -Wformat -Werror=format-security"
When I now pass this output manually (copy&paste) to ./configure it works:
./configure CFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong -Wformat -Werror=format-security" CPPFLAGS="-Wdate-time -D_FORTIFY_SOURCE=2" CXXFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong -Wformat -Werror=format-security" FCFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong" FFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong" GCJFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong" LDFLAGS="-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now" OBJCFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong -Wformat -Werror=format-security" OBJCXXFLAGS="-g -O2 -fdebug-prefix-map=/home/user/popt-1.16=. -fstack-protector-strong -Wformat -Werror=format-security"
checking build system type... x86_64-unknown-linux-gnu
[...]
How can I invoke dpkg-buildflags to automatically pass its flags to ./configure?
Why does manual copy&paste work but not the other approach?
Why does manual copy&paste work but not the other approach?
This is related to argument escaping. Consider the following function:
# This function simply prints its arguments, each on separate line
test () {
for arg in "$#"; do
echo "$arg"
done
}
This is what happens when you pass the output of dpkg-buildflags manually:
$ test ROSES=red VIOLETS=blue CFLAGS="-O0 -ggdb"
ROSES=red
VIOLETS=blue
CFLAGS=-O0 -ggdb
Note that CFLAGS=-O0 -ggdb is seen as a single argument, so escaping works.
Now let's try to evaluate an inline expression:
$ test $(echo ROSES=red VIOLETS=blue CFLAGS="-O0 -ggdb")
ROSES=red
VIOLETS=blue
CFLAGS=-O0
-ggdb
Boom! Somehow CFLAGS=-O0 -ggdb got broken into CFLAGS=-O0 and -ggdb. The same is happening in your case: the value of CFLAGS gets split into multiple arguments, and indeed -O2 is not a valid option. This is why you're getting an error.
How can I invoke dpkg-buildflags to automatically pass its flags to ./configure?
You can work this around using a pipe and xargs command:
dpkg-buildflags --export=cmdline | xargs ./configure
This solution works for me.

How to build dynamic-library in qt static build

I try to build my dynamic library project in linux. I build my qt library in static. But if i use it to build my proj, qt creator builds only libname.a file. However if i use original qt library qt creator builds libname.so file. Wats is the problem?
Pro file.
`#------------------------------------------------- # #
Project created by QtCreator 2020-10-22T07:46:59
# #-------------------------------------------------
QT -= gui TARGET = up_api_send
TEMPLATE = lib
DEFINES += UP_API_SEND_LIBRARY
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += up_api_send.cpp
HEADERS += up_api_send.h\
up_api_send_global.h
unix { target.path = /usr/lib INSTALLS += target }
unix:!macx: LIBS += -L$$PWD/libplot/ -lplot2
INCLUDEPATH += $$PWD/headerplot
DEPENDPATH += $$PWD/headerplot`
Build log.
Original library.
16:04:07: Start build project up_api_send...
16:04:07: Starts: «/opt/Qt5.7.1/5.7/gcc_64/bin/qmake» /root/QT/QT_SO/up_api_send/up_api_send.pro -spec linux-g++
Info: creating stash file /root/QT/QT_SO/build-up_api_send-Desktop_Qt_5_7_1_GCC_64bit-Release/.qmake.stash
16:04:07: Процесс «/opt/Qt5.7.1/5.7/gcc_64/bin/qmake» success.
16:04:07: Starts: «/usr/bin/make» qmake_all
make: Target `qmake_all' action is not required.
16:04:07: Process «/usr/bin/make» success.
16:04:07: Starts: «/usr/bin/make»
g++ -c -pipe -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DUP_API_SEND_LIBRARY -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I../up_api_send -I. -I../up_api_send/headerplot -I/opt/Qt5.7.1/5.7/gcc_64/include -I/opt/Qt5.7.1/5.7/gcc_64/include/QtCore -I. -I/opt/Qt5.7.1/5.7/gcc_64/mkspecs/linux-g++ -o up_api_send.o ../up_api_send/up_api_send.cpp
../up_api_send/up_api_send.cpp: In member function ‘bool DgateTrans::PLOT_sendMsg(const char*, const char*, int)’:
../up_api_send/up_api_send.cpp:143:19: warning: unused variable ‘tid’ [-Wunused-variable]
/opt/Qt5.7.1/5.7/gcc_64/bin/moc -DUP_API_SEND_LIBRARY -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I/opt/Qt5.7.1/5.7/gcc_64/mkspecs/linux-g++ -I/root/QT/QT_SO/up_api_send -I/root/QT/QT_SO/up_api_send/headerplot -I/opt/Qt5.7.1/5.7/gcc_64/include -I/opt/Qt5.7.1/5.7/gcc_64/include/QtCore -I. -I/usr/include/c++/4.7 -I/usr/include/c++/4.7/x86_64-linux-gnu -I/usr/include/c++/4.7/backward -I/usr/lib/gcc/x86_64-linux-gnu/4.7/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include ../up_api_send/up_api_send.h -o moc_up_api_send.cpp
g++ -c -pipe -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DUP_API_SEND_LIBRARY -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I../up_api_send -I. -I../up_api_send/headerplot -I/opt/Qt5.7.1/5.7/gcc_64/include -I/opt/Qt5.7.1/5.7/gcc_64/include/QtCore -I. -I/opt/Qt5.7.1/5.7/gcc_64/mkspecs/linux-g++ -o moc_up_api_send.o moc_up_api_send.cpp
rm -f libup_api_send.so.1.0.0 libup_api_send.so libup_api_send.so.1 libup_api_send.so.1.0
g++ -Wl,-O1 -Wl,-rpath,/opt/Qt5.7.1/5.7/gcc_64/lib -shared -Wl,-soname,libup_api_send.so.1 -o libup_api_send.so.1.0.0 up_api_send.o moc_up_api_send.o -L/root/QT/QT_SO/up_api_send/libplot/ -lplot2 -L/opt/Qt5.7.1/5.7/gcc_64/lib -lQt5Core -lpthread
ln -s libup_api_send.so.1.0.0 libup_api_send.so
ln -s libup_api_send.so.1.0.0 libup_api_send.so.1
ln -s libup_api_send.so.1.0.0 libup_api_send.so.1.0
16:04:09: Process «/usr/bin/make» success.
16:04:09: Time passed: 00:02.
Build log. Static library.
16:05:00: Start build project up_api_send...
16:05:00: Settings not changed, stage qmake is skipped.
16:05:00: Starts: «/usr/bin/make»
g++ -c -pipe -O2 -fPIC -std=gnu++11 -Wall -W -D_REENTRANT -DUP_API_SEND_LIBRARY -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I../up_api_send -I. -I../up_api_send/headerplot -I/usr/local/Qt-5.7.1/include -I/usr/local/Qt-5.7.1/include/QtCore -I. -I/usr/local/Qt-5.7.1/mkspecs/linux-g++ -o up_api_send.o ../up_api_send/up_api_send.cpp
../up_api_send/up_api_send.cpp: In member function ‘bool DgateTrans::PLOT_sendMsg(const char*, const char*, int)’:
../up_api_send/up_api_send.cpp:143:19: warning: unused variable ‘tid’ [-Wunused-variable]
/usr/local/Qt-5.7.1/bin/moc -DUP_API_SEND_LIBRARY -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I/usr/local/Qt-5.7.1/mkspecs/linux-g++ -I/root/QT/QT_SO/up_api_send -I/root/QT/QT_SO/up_api_send/headerplot -I/usr/local/Qt-5.7.1/include -I/usr/local/Qt-5.7.1/include/QtCore -I. -I/usr/include/c++/4.7 -I/usr/include/c++/4.7/x86_64-linux-gnu -I/usr/include/c++/4.7/backward -I/usr/lib/gcc/x86_64-linux-gnu/4.7/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include ../up_api_send/up_api_send.h -o moc_up_api_send.cpp
g++ -c -pipe -O2 -fPIC -std=gnu++11 -Wall -W -D_REENTRANT -DUP_API_SEND_LIBRARY -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I../up_api_send -I. -I../up_api_send/headerplot -I/usr/local/Qt-5.7.1/include -I/usr/local/Qt-5.7.1/include/QtCore -I. -I/usr/local/Qt-5.7.1/mkspecs/linux-g++ -o moc_up_api_send.o moc_up_api_send.cpp
rm -f libup_api_send.a
ar cqs libup_api_send.a up_api_send.o moc_up_api_send.o
16:05:02: Process «/usr/bin/make» success.
16:05:02: Time passed: 00:02.
Maybe i wrote wrong parameters when build qt library?
./configure -platform linux-g++
-release
-static
-fontconfig
-opensource
-confirm-license
-gtkstyle
-no-webkit
-nomake demos
-nomake examples

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.

Port Mongoose web server to FreeBSD

I was wondering if anyone had success in porting the Linux versions of the Mongoose web server or the Civetweb web server to FreeBSD. I've tried to do a Make, but there is a compatibility issue between Linux and BSD, in that it cannot find -ldl. I've seen reference to this in a google search, and the recommended solution is probably beyond my skill level.
Linux uses GNU make by default. Its makefiles tend to be incompatible with FreeBSD's make. So you need to install GNU make using the /usr/ports/devel/gmake port. The program is called gmake on FreeBSD.
So for mongoose, download the latest mongoose source from github.
Then install GNU make using the /usr/ports/devel/gmake port. Unpack mongoose;
> tar xf mongoose-5.3.tar.gz
> cd mongoose-5.3/examples
Then compile it;
> gmake
cc hello.c ../mongoose.c -o hello -W -Wall -I.. -pthread -g -pipe
perl mkdata.pl websocket.html > websocket_html.c
cc websocket.c websocket_html.c ../mongoose.c -o websocket -W -Wall -I.. -pthread -g -pipe
cc server.c ../mongoose.c -o server -W -Wall -I.. -pthread -g -pipe
cc post.c ../mongoose.c -o post -W -Wall -I.. -pthread -g -pipe
cc multi_threaded.c ../mongoose.c -o multi_threaded -W -Wall -I.. -pthread -g -pipe
cc upload.c ../mongoose.c -o upload -W -Wall -I.. -pthread -g -pipe
cc auth.c ../mongoose.c -o auth -W -Wall -I.. -pthread -g -pipe
Civetweb also compiles without errors using gmake.

Resources