Skip to content

Instantly share code, notes, and snippets.

@likema
Last active July 11, 2025 00:16
Show Gist options
  • Save likema/f5c04dad837d2f5068ae7a8860c180e7 to your computer and use it in GitHub Desktop.
Save likema/f5c04dad837d2f5068ae7a8860c180e7 to your computer and use it in GitHub Desktop.
CMake macro to detect glibc version by filename.
# - Check glibc version
# CHECK_GLIBC_VERSION()
#
# Once done this will define
#
# GLIBC_VERSION - glibc version
#
MACRO (CHECK_GLIBC_VERSION)
EXECUTE_PROCESS (
COMMAND ${CMAKE_C_COMPILER} -print-file-name=libc.so.6
OUTPUT_VARIABLE GLIBC
OUTPUT_STRIP_TRAILING_WHITESPACE)
GET_FILENAME_COMPONENT (GLIBC ${GLIBC} REALPATH)
GET_FILENAME_COMPONENT (GLIBC_VERSION ${GLIBC} NAME)
STRING (REPLACE "libc-" "" GLIBC_VERSION ${GLIBC_VERSION})
STRING (REPLACE ".so" "" GLIBC_VERSION ${GLIBC_VERSION})
IF (NOT GLIBC_VERSION MATCHES "^[0-9.]+$")
MESSAGE (FATAL_ERROR "Unknown glibc version: ${GLIBC_VERSION}")
ENDIF (NOT GLIBC_VERSION MATCHES "^[0-9.]+$")
ENDMACRO (CHECK_GLIBC_VERSION)
@likema
Copy link
Author

likema commented Apr 25, 2019

Check glibc version if it is composed of 0, ..., 9 and .

@eyalroz
Copy link

eyalroz commented Jan 2, 2025

This will fail if the base file is libc.so.6 ... which it often is.

@likema
Copy link
Author

likema commented Jan 6, 2025

This will fail if the base file is libc.so.6 ... which it often is.

What do you mean?

@eyalroz
Copy link

eyalroz commented Jan 6, 2025

What do you mean?

I mean, that the filename doesn't include the glibc version - and this module assumes that it does and tries to parse the name to get the version.

@likema
Copy link
Author

likema commented Jan 6, 2025

Any suggestions ?

@eyalroz
Copy link

eyalroz commented Jan 6, 2025

Well, one thing which often works - not sure whether it always works - is run libc.so.6. Yes, it's typically executable - and prints version information.

@whnbaek
Copy link

whnbaek commented Jul 2, 2025

It seems STRING (REPLACE "libc-" "" GLIBC_VERSION ${GLIBC_VERSION}) causes the problem.
I resolved this by removing the dash character like STRING (REPLACE "libc" "" GLIBC_VERSION ${GLIBC_VERSION}).
I'm not sure whether this is acceptable for all versions of GLIBC.

@likema
Copy link
Author

likema commented Jul 3, 2025

It seems STRING (REPLACE "libc-" "" GLIBC_VERSION ${GLIBC_VERSION}) causes the problem. I resolved this by removing the dash character like STRING (REPLACE "libc" "" GLIBC_VERSION ${GLIBC_VERSION}). I'm not sure whether this is acceptable for all versions of GLIBC.

Which distribution is your OS ? Ubuntu or Redhat ?

Can you show me ?

realpath `gcc -print-file-name=libc.so.6`

@likema
Copy link
Author

likema commented Jul 3, 2025

Well, one thing which often works - not sure whether it always works - is run libc.so.6. Yes, it's typically executable - and prints version information.

It does not work for cross compiler, such as aarch64

Maybe you can implement CMake macro like

objdump -T `gcc -print-file-name=libc.so.6` \
  | grep -o 'GLIBC_[0-9]\+\(\.[0-9]\+\)*' \
  | sort -ruV \
  | head -1

@whnbaek
Copy link

whnbaek commented Jul 3, 2025

Well, one thing which often works - not sure whether it always works - is run libc.so.6. Yes, it's typically executable - and prints version information.

It does not work for cross compiler, such as aarch64

Maybe you can implement CMake macro like

objdump -T `gcc -print-file-name=libc.so.6` \
  | grep -o 'GLIBC_[0-9]\+\(\.[0-9]\+\)*' \
  | sort -ruV \
  | head -1
realpath `gcc -print-file-name=libc.so.6`
/usr/lib/x86_64-linux-gnu/libc.so.6

Or adding one more line would be fine, like this.

MACRO (CHECK_GLIBC_VERSION)
    EXECUTE_PROCESS (
        COMMAND ${CMAKE_C_COMPILER} -print-file-name=libc.so.6
	OUTPUT_VARIABLE GLIBC
	OUTPUT_STRIP_TRAILING_WHITESPACE)

    GET_FILENAME_COMPONENT (GLIBC ${GLIBC} REALPATH)
    GET_FILENAME_COMPONENT (GLIBC_VERSION ${GLIBC} NAME)
    STRING (REPLACE "libc-" "" GLIBC_VERSION ${GLIBC_VERSION})
    STRING (REPLACE "libc" "" GLIBC_VERSION ${GLIBC_VERSION})  # added
    STRING (REPLACE ".so" "" GLIBC_VERSION ${GLIBC_VERSION})
    IF (NOT GLIBC_VERSION MATCHES "^[0-9.]+$")
        MESSAGE (FATAL_ERROR "Unknown glibc version: ${GLIBC_VERSION}")
    ENDIF (NOT GLIBC_VERSION MATCHES "^[0-9.]+$")
ENDMACRO (CHECK_GLIBC_VERSION)

@likema
Copy link
Author

likema commented Jul 3, 2025

Even added this line, it seems that only the major version of glibc can be extracted

@whnbaek
Copy link

whnbaek commented Jul 11, 2025

MACRO (CHECK_GLIBC_VERSION)
	message("Checking GLIBC Version.")
	EXECUTE_PROCESS (
		COMMAND ${CMAKE_C_COMPILER} -print-file-name=libc.so.6
		OUTPUT_VARIABLE GLIBC
		OUTPUT_STRIP_TRAILING_WHITESPACE)

	GET_FILENAME_COMPONENT (GLIBC ${GLIBC} REALPATH)
	EXECUTE_PROCESS (
		COMMAND ${GLIBC}
		OUTPUT_VARIABLE GLIBC_OUTPUT
		ERROR_QUIET
		OUTPUT_STRIP_TRAILING_WHITESPACE)
	STRING (REGEX MATCH "release version ([0-9]+\\.[0-9]+)" GLIBC_VERSION ${GLIBC_OUTPUT})
	STRING (REPLACE "release version " "" GLIBC_VERSION ${GLIBC_VERSION})
	IF (NOT GLIBC_VERSION MATCHES "^[0-9.]+$")
		MESSAGE (FATAL_ERROR "Unknown glibc version: ${GLIBC_VERSION}")
	ENDIF (NOT GLIBC_VERSION MATCHES "^[0-9.]+$")
ENDMACRO (CHECK_GLIBC_VERSION)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment