0

I am trying to build and run a Flutter application on Linux, but I am encountering issues with CMake. The application runs successfully on Google Chrome, but I'm having trouble getting it to work on Linux.

I have followed the steps provided in the Flutter documentation and made necessary modifications to the CMakeLists.txt file as per the suggestions I received. Here are the steps I have taken:

  1. I installed Flutter and ensured that it is set up correctly, as I can run the application on Google Chrome using flutter run -d chrome.

  2. I modified the CMakeLists.txt file in the linux directory of my Flutter project to set the C++ compiler and standard library explicitly. Here are the modifications I made:

    # Project-level configuration.
    cmake_minimum_required(VERSION 3.10)
    project(runner LANGUAGES CXX)
    
    # Set the C++ compiler
    set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
    
    # Static link the standard C++ library
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
    
    # Set C++17 standard
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
    
    # Rest of the CMakeLists.txt content...
    
    
  3. I also ran the following commands in the linux directory to build the project.

    cd linux
    rm -rf build
    mkdir build
    cd build
    cmake ..
    cmake --build .
    
    

    However, during the build process, I encounter the following error:

    CMake Error at /usr/share/cmake-3.22/Modules/CMakeTestCXXCompiler.cmake:62 (message):
      The C++ compiler
    
        "/usr/bin/clang++"
    
      is not able to compile a simple test program.
    
      It fails with the following output:
    
        Change Dir: /home/farhan/App/flutter_application3/build/linux/x64/release/CMakeFiles/CMakeTmp
    
        Run Build Command(s):/usr/bin/ninja cmTC_47048 && [1/2] Building CXX object CMakeFiles/cmTC_47048.dir/testCXXCompiler.cxx.o
        [2/2] Linking CXX executable cmTC_47048
        FAILED: cmTC_47048 
        : && /usr/bin/clang++   CMakeFiles/cmTC_47048.dir/testCXXCompiler.cxx.o -o cmTC_47048   && :
        /usr/bin/ld: cannot find -lstdc++: No such file or directory
        clang: error: linker command failed with exit code 1 (use -v to see invocation)
        ninja: build stopped: subcommand failed.
    
    

I am unable to resolve this issue, and I am not sure what's causing the problem. I have verified that I have the necessary dependencies installed on my Linux system.

Could anyone please guide me on how to properly set up CMake and resolve this issue so that I can build and run my Flutter application on Linux successfully?

  • `set(CMAKE_CXX_COMPILER "/usr/bin/clang++")` why do you want to use `clang` compiler instead of `gcc`? `/usr/bin/ld: cannot find -lstdc++` - looks like it cannot find standard library, either not installed, or it tried to find it under wrong path. – pptaszni Jul 26 '23 at 11:49
  • I have tried the both gcc and clang.I don't know what to do further – Farhan Momin Jul 26 '23 at 11:59
  • 1
    Instead of **re-asking** [the question](https://stackoverflow.com/questions/76764942/cmake-error-cmake-cxx-compiler-not-set-even-after-explicitly-specifying-the-c), it is better to update the old one. – Tsyvarev Jul 26 '23 at 12:05
  • Note, that setting variable CMAKE_CXX_COMPILER **after** the `project()` call is plain **wrong**: https://stackoverflow.com/a/63944545/3440745. – Tsyvarev Jul 26 '23 at 12:07
  • "I have tried the both gcc and clang" - mmm OK, and what was the problem with gcc? Can you compile simple `g++ hello_world.cpp -o hello_world` ? – pptaszni Jul 26 '23 at 12:26
  • Yes i am able to compile hello_world.cpp – Farhan Momin Jul 26 '23 at 12:34

0 Answers0