KMB: pass top-level project compiler to external project #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Running "brew install gcc" installs gcc-10 and g++-10 in /usr/local/bin, leaving the XCode CLT gcc and g++ commands in /usr/bin. This results in "cmake .." in the build directory using the old version of the compiler.
If instead we run "CC=gcc-10 CXX=g++-10 cmake .." or "cmake -DCMAKE_C_COMPILER=gcc-10 -DCMAKE_CXX_COMPILER=g++-10 .." then the top-level project picks up the new compiler, but when gtest is downloaded and configured by the "make" command, it picks up the old compiler as it uses the default "cc" and "c++" compiler commands. This results in the TextExample link phase failing with undefined symbols because the two compilers use different name-mangling on C++ function names.
We can work around the problem by running "CC=gcc-10 CXX=g++-10 make", but this is nasty. The gtest makefiles ought to be generated correctly in the first place. This is achieved by passing the compiler name and flags from the top-level project in the ExternalProject_Add() call for gtest. This technique will have wider application when multiple build directories are generated for a top-level project, supporting different cross-compiler targets. In this case, external projects will need to be compiled with the correct compiler and flags, and working around the problem in the build command won't be practical.
Also worth noting that "make" runs the XCode CLT command from /usr/bin. If "brew install make" is used then the command has to be run as "gmake" to get the version in /usr/local/bin. Alternatively, the PATH can be overridden to put the homebrew version at the front using "export PATH=$(brew --prefix make)/libexec/gnubin:$PATH" in the ~/.bash_profile file or similar.