[CMake] CHECK_CXX_COMPILER_FLAG doesn't give correct result for -fsanitize=address

Marc CHEVRIER marc.chevrier at gmail.com
Fri Oct 25 06:36:01 EDT 2019


This is the expected behaviour.

As you already discovered, it is possible to control the compilation and link steps with variables as described in https://cmake.org/cmake/help/latest/module/CheckCXXSourceCompiles.html#module:CheckCXXSourceCompiles.

To specify, in a more clean way, link options, use preferably the variable CMAKE_REQUIRED_LINK_OPTIONS.

Le 24 oct. 2019 à 22:42 +0200, Michael Ellery <mellery451 at gmail.com>, a écrit :
> I’ve dealt with that using something like this:
>
> set (_saved_CRL ${CMAKE_REQUIRED_LIBRARIES})
> set (CMAKE_REQUIRED_LIBRARIES “-fsanitize=address;asan")
> check_cxx_compiler_flag (-fsanitize=address COMPILER_SUPPORTS_SAN)
> set (CMAKE_REQUIRED_LIBRARIES ${_saved_CRL})
>
> the second item in CMAKE_REQUIRED_LIBRARIES is only needed when using gcc which also requires a -lasan when linking (clang does not need this)
>
> > On Oct 24, 2019, at 12:47 PM, Turner, Adrian Keith via CMake <cmake at cmake.org> wrote:
> >
> > Hi CMake Mailing List,
> >
> > I am using the CHECK_CXX_COMPILER_FLAG directive in a CMake file to detect whether the compiler uses the "-fsanitize=address" compiler flag. I'm using the g++ 7.3.0 compiler which supports this flag but the CHECK_CXX_COMPILER_FLAG directive incorrectly determines that this flag is not supported.
> >
> > To test this I tried compiling the following program:
> > int main() {};
> >
> > This compiled with the following commands:
> > > g++ -fsanitize=address -c test.cpp
> > > g++ -fsanitize=address test.o -o test.exe
> >
> > And fails with the same error message as CHECK_CXX_COMPILER_FLAG when I compile with:
> > > g++ -fsanitize=address -c test.cpp
> > > g++ test.o -o test.exe
> >
> > producing the following message:
> > Undefined symbols for architecture x86_64:
> > "___asan_init", referenced from:
> > __GLOBAL__sub_I_00099_0_test.cpp in test.o
> > "___asan_version_mismatch_check_v8", referenced from:
> > __GLOBAL__sub_I_00099_0_test.cpp in test.o
> > ld: symbol(s) not found for architecture x86_64
> > collect2: error: ld returned 1 exit status
> >
> > It seems that CHECK_CXX_COMPILER_FLAG doesn't add the flag to the linking command when it tests compilation. Is this a bug with CHECK_CXX_COMPILER_FLAG or is there something I'm missing?
> >
> > Many thanks
> > Adrian Turner
> >
> >
> > --
> >
> > Powered by www.kitware.com
> >
> > Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> >
> > Kitware offers various services to support the CMake community. For more information on each offering, please visit:
> >
> > CMake Support: http://cmake.org/cmake/help/support.html
> > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> > CMake Training Courses: http://cmake.org/cmake/help/training.html
> >
> > Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> >
> > Follow this link to subscribe/unsubscribe:
> > https://cmake.org/mailman/listinfo/cmake
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191025/d4f0ea4b/attachment.html>


More information about the CMake mailing list