You are not logged in.

#1 2022-11-13 20:53:17

patatahooligan
Member
Registered: 2022-11-13
Posts: 1

Should I manually use CXXFLAGS when building a package with g++

I used to think that CFLAGS and CXXFLAGS defined in makepkg.conf are passed to gcc/g++, but I looked up the variables online and they seem to be used by make, not gcc/g++. So now I'm not sure if they are being passed. This is an issue because as a user I expect my makepkg.conf to enable compiler optimizations. So my questions are

  • Am I correct that these variables are being ignored in PKGBUILDs that directly invoke gcc/g++?

  • If yes, what should I do about it? Should my PKGBUILD use them manually? eg

    g++ ${CXXFLAGS} ...

Offline

#2 2022-11-14 07:06:23

mpan
Member
Registered: 2012-08-01
Posts: 1,188
Website

Re: Should I manually use CXXFLAGS when building a package with g++

Note: this is an issue with understanding the typical C and C++ toolchain use, in no way related to Arch Linux or makepkg. So, for the most of this response, ignore existence of makepkg.

When you invoke a compiler (gcc, g++) or a linker (ld; though often also invoked through gcc) to turn source code into a program, you must pass a bunch of arguments to them. For example a typical invocation to turn sources “foo.c”, “bar.c” and “baz.c” into “program”, with optimization level 3 and various warnings enabled, requiring library “libcool” and “libhappy”:

gcc -c -Wall -O3 -o foo.o foo.c
gcc -c -Wall -O3 -o bar.o bar.c
gcc -c -Wall -O3 -o baz.o baz.c
gcc -lcool -lhappy -o program foo.o bar.o baz.o

As you may noticed:

  • Many options are identical across multiple invocations.

  • Many options are something that is likely to be parametrized (e.g. libraries used).

When using a build tool (like pure Make, GNU Autotools or some other systems), there will often be tens if not hundreds of such almost identical invocations and many things need to be easily configured without having to manually edit each call. For that variables are used.

Over decades some of those variables got standardized: `CFLAGS` and `CXXFLAGS` are mong them. Those are set by the user invoking the build system and are passed down the chain, ending up in invocations of particular tools (like gcc or g++).

Now, putting makepkg back in the picture: since those flags are used by particular build systems, your assertion is correct. If you do not employ such system, those variables will not be used. Makepkg is unaware what g++ is and does no magic there. That moves us to the second question and the answer is: yes. If you build software missing a Makefile (or similar) and want to support flags meant for Make, you must handle those variables manually.


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

Board footer

Powered by FluxBB