cmake shows -static-libstdc++ file missing when it existed on the LINUX system.

Below code failed to run, why? How to fix?
– Performing Test STATIC_SYSTEM_LIBS
– Performing Test STATIC_SYSTEM_LIBS - Failed

  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++ -Wl,-z,defs")
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
  check_cxx_source_runs("int main() { return 0; }" STATIC_SYSTEM_LIBS)
  if (NOT STATIC_SYSTEM_LIBS)
     message(WARNING "You are missing one or more static system libraries that will be linked by -static-libgcc -static-libstdc++")
  endif()

There should be more context about the error in CMakeFiles/CMakeError.log or the like (they should all be under that directory though).

There are no such log existed. below are all the file lists:
cmake.check_cache
Makefile.cmake
Makefile2
nfiq2api.dir
progress.marks
nfiq2.dir
OpenCV.dir
TargetDirectories.txt
CMakeRuleHashes.txt
CMakeDirectoryInformation.cmake
CMakeConfigureLog.yaml
CMakeScratch
3.26.3
feature_tests.bin
pkgRedirects

When I extracted that above part and run that alone, it works.
But when it embedded in a big CMakeLists.txt, it simple fails.

Can you try adding other parts of the “big” CMake code piece by piece until it breaks? That should get us a simpler test case to look at.

Thanks for the fast responses Ben.
Actually I am just building the NFIQ2 library from gitHub.

I am following the step below, but failed.
git clone --recursive GitHub - usnistgov/NFIQ2: Optical live-scan and ink fingerprint image quality assessment tool
cd NFIQ2
mkdir build
cd build
cmake … -DBUILD_NFIQ2_CLI=OFF
cmake --build .

is it possible for you to try it in your env?
Many thanks,
Joe

On my machine, I see:

    buildResult:
      variable: "STATIC_SYSTEM_LIBS_COMPILED"
      cached: true
      stdout: |
        Change Dir: '/home/boeckb/misc/code/NFIQ2/build/CMakeFiles/CMakeScratch/TryCompile-BmQw0N'
        
        Run Build Command(s): /home/boeckb/.local/bin/ninja -v cmTC_29159
        [1-1->1/2@52.6] /usr/lib64/ccache/c++ -DSTATIC_SYSTEM_LIBS   -o CMakeFiles/cmTC_29159.dir/src.cxx.o -c /home/boeckb/misc/code/NFIQ2/build/CMakeFiles/CMakeScratch/TryCompile-BmQw0N/src.cxx
        [0-1->2/2@74.1] : && /usr/lib64/ccache/c++  -static-libgcc -static-libstdc++   -rdynamic CMakeFiles/cmTC_29159.dir/src.cxx.o -o cmTC_29159   && :
        FAILED: cmTC_29159 
        : && /usr/lib64/ccache/c++  -static-libgcc -static-libstdc++   -rdynamic CMakeFiles/cmTC_29159.dir/src.cxx.o -o cmTC_29159   && :
        /usr/bin/ld: cannot find -lstdc++: No such file or directory
        collect2: error: ld returned 1 exit status
        ninja: build stopped: subcommand failed.
        
      exitCode: 1

in CMakeFiles/CMakeConfigureLog.yaml. I indeed do not have the static libraries installed.

On my, I saw CMakeFiles/CMakeConfigureLog.yaml:
But if I try the code snippet on the first comment, I got success! How to fix?

buildResult:
variable: “STATIC_SYSTEM_LIBS_COMPILED”
cached: true
stdout: |
Change Dir: /home/autobld/j/NFIQ2/build/CMakeFiles/CMakeScratch/TryCompile-iK37Wm

    Run Build Command(s):/opt/cmake-3.26.3-linux-x86_64/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_dd0d2/fast && /usr/bin/gmake  -f CMakeFiles/cmTC_dd0d2.dir/build.make CMakeFiles/cmTC_dd0d2.dir/build
    gmake[1]: Entering directory `/home/autobld/j/NFIQ2/build/CMakeFiles/CMakeScratch/TryCompile-iK37Wm'
    Building CXX object CMakeFiles/cmTC_dd0d2.dir/src.cxx.o
    /usr/bin/c++ -DSTATIC_SYSTEM_LIBS   -o CMakeFiles/cmTC_dd0d2.dir/src.cxx.o -c /home/autobld/j/NFIQ2/build/CMakeFiles/CMakeScratch/TryCompile-iK37Wm/src.cxx
    Linking CXX executable cmTC_dd0d2
    /opt/cmake-3.26.3-linux-x86_64/bin/cmake -E cmake_link_script CMakeFiles/cmTC_dd0d2.dir/link.txt --verbose=1
    /usr/bin/c++  -static-libgcc -static-libstdc++  -rdynamic CMakeFiles/cmTC_dd0d2.dir/src.cxx.o -o cmTC_dd0d2
    /usr/bin/ld: cannot find -lstdc++
    collect2: error: ld returned 1 exit status
    gmake[1]: *** [cmTC_dd0d2] Error 1
    gmake[1]: Leaving directory `/home/autobld/j/NFIQ2/build/CMakeFiles/CMakeScratch/TryCompile-iK37Wm'
    gmake: *** [cmTC_dd0d2/fast] Error 2

  exitCode: 2
runResult:
  variable: "STATIC_SYSTEM_LIBS_EXITCODE"
  cached: true

That doesn’t look like success to me. Where is it saying that it succeeded?

Make the CMakeLists.txt as simple as below:

include(CheckCXXSourceRuns)
cmake_minimum_required(VERSION 3.5)
project(HelloWorld C CXX)
include_directories(${PROJECT_SOURCE_DIR})
add_executable(hello hello.cpp)
add_executable(hello2 hello.c)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++ -Wl,-z,defs")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
check_cxx_source_runs ("int main() { return 0; }" STATIC_SYSTEM_LIBS)
if (NOT STATIC_SYSTEM_LIBS)
    message(WARNING "You are missing one or more static system libraries that will be linked by -static-libgcc -static-libstdc++")
endif()

hello.c

#include "stdio.h"

int main()
{
    printf("this is just a test\n");
    return 0;
}

hello.cpp:

#include <iostream>

int main()
{
    for (int i = 0; i < Times; i++)
    {
        std::cout << "Hello, world!" << std::endl;
    }
    return 0;
}

and run cmake -B build .
and it got successs.
Can you please try the above?

Is it really expected to work to compile the hello2 C program with a C++-specific compiler argument like -static-libstdc++ ?

You did not set CMAKE_REQUIRED_LINK_OPTIONS as needed for check_cxx_source_runs() to test something useful.

I think so, the core library is c++, but it will be exposed as C API as well.

It was running OK in my test env for this example.

Ok, so the build worked. Did the configure check for the static system libraries pass?

Yes, now the question is why it failed on NFIQ2 source code build? I extracted that part of code and it succeeded as both of us tested. Why failed there?

It did not pass for me; I also saw – Performing Test STATIC_SYSTEM_LIBS - Failed because I do not have the libraries referenced installed on my machine.

Can you please install and try it again?

I have yet to see any output that shows the try_compile succeeding on your setup. You’ve said it builds, but this does not mean the try_compile worked as it only raises a warning, not an error.