Skip to content

Xmake fails to build c++ under clang due to wrong compiler usage #6803

@bugsnotabunny

Description

@bugsnotabunny

Xmake Version

v3.0.1+19800101

Operating System Version and Architecture

NixOS 25.05 (Warbler) x86_64

Describe Bug

Xmake tends to use gcc and clang instead of clang++ and g++ respectively. On clang this means that c++ std library is not accessible (gcc somehow works around that)

As I believe, my pr should fix this issue

Expected Behavior

Xmake prioritizes g++ or clang++ for building c++ code

Project Configuration

// xmake.lua
set_warnings("all", "extra", "pedantic")
set_languages("c++20")

target("main")
    set_kind("binary")
    add_files("./*.cpp")
// main.cpp
#include <iostream>

int main() { std::cout << "hello\n"; }

Additional Information and Error Logs

Here is xmake output with clang:

banan@nixos [17:43:54] [~/repos/playground] 
-> % xmake f --toolchain=clang --mode=debug && xmake run -vD
checking for platform ... linux
checking for architecture ... x86_64
checking for clang ... /nix/store/67pdq2cm22ypnkczf8ll611v1p469wdm-clang-wrapper-19.1.7/bin/clang
checking for the c++ compiler (cxx) ... clang
checking for /nix/store/67pdq2cm22ypnkczf8ll611v1p469wdm-clang-wrapper-19.1.7/bin/clang ... ok
checking for flags (-fPIC) ... ok
> clang "-fPIC" "-Qunused-arguments" "-m64"
checking for flags (-std=c++20) ... ok
> clang "-std=c++20" "-Qunused-arguments" "-m64"
[ 23%]: cache compiling.debug main.cpp
/nix/store/67pdq2cm22ypnkczf8ll611v1p469wdm-clang-wrapper-19.1.7/bin/clang -c -Qunused-arguments -m64 -Wall -Wextra -Wpedantic -std=c++20 -o build/.objs/main/linux/x86_64/debug/main.cpp.o main.cpp
checking for flags (-MMD -MF) ... ok
> clang "-MMD" "-MF" "/dev/null" "-Qunused-arguments" "-m64"
checking for flags (-fdiagnostics-color=always) ... ok
> clang "-fdiagnostics-color=always" "-Qunused-arguments" "-m64"
checking for flags (-Wno-gnu-line-marker -Werror) ... ok
> clang "-Wno-gnu-line-marker" "-Werror" "-Qunused-arguments" "-m64"
error: @programdir/core/main.lua:329: @programdir/core/sandbox/modules/import/core/base/task.lua:65: @programdir/actions/build/main.lua:146: @programdir/modules/async/runjobs.lua:331: @programdir/modules/private/action/build/object.lua:100: @programdir/modules/core/tools/gcc.lua:1035: main.cpp:1:10: fatal error: 'iostream' file not found
    1 | #include <iostream>
      |          ^~~~~~~~~~
1 error generated.
stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:1075]:
    [@programdir/modules/core/tools/gcc.lua:1035]: in function 'catch'
    [@programdir/core/sandbox/modules/try.lua:123]: in function 'try'
    [@programdir/modules/core/tools/gcc.lua:976]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:246]:
    [@programdir/core/tool/compiler.lua:288]: in function 'compile'
    [@programdir/modules/private/action/build/object.lua:100]: in function 'script'
    [@programdir/modules/private/action/build/object.lua:131]: in function 'build_object'
    [@programdir/modules/private/action/build/object.lua:171]: in function 'jobfunc'
    [@programdir/modules/async/runjobs.lua:247]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:246]: in function 'trycall'
    [@programdir/core/sandbox/modules/try.lua:117]: in function 'try'
    [@programdir/modules/async/runjobs.lua:230]: in function 'cotask'
    [@programdir/core/base/scheduler.lua:406]:

stack traceback:
	[C]: in function 'error'
	@programdir/core/base/os.lua:1075: in function 'os.raiselevel'
	(...tail calls...)
	@programdir/core/main.lua:329: in upvalue 'cotask'
	@programdir/core/base/scheduler.lua:406: in function <@programdir/core/base/scheduler.lua:399>

This works fine with gcc even though the wrong compiler is being used:

banan@nixos [17:44:00] [~/repos/playground] 
-> % xmake f --toolchain=gcc --mode=debug && xmake run -vD  
checking for platform ... linux
checking for architecture ... x86_64
checking for gcc ... /nix/store/67x7pknz0qa2j16x02idf0x98lpcspah-gcc-wrapper-14.3.0/bin/gcc
checking for the c++ compiler (cxx) ... gcc
checking for /nix/store/67x7pknz0qa2j16x02idf0x98lpcspah-gcc-wrapper-14.3.0/bin/gcc ... ok
checking for flags (-fPIC) ... ok
> gcc "-fPIC" "-m64"
checking for flags (-std=c++20) ... ok
> gcc "-std=c++20" "-m64"
[ 23%]: cache compiling.debug main.cpp
/nix/store/67x7pknz0qa2j16x02idf0x98lpcspah-gcc-wrapper-14.3.0/bin/gcc -c -m64 -Wall -Wextra -Wpedantic -std=c++20 -o build/.objs/main/linux/x86_64/debug/main.cpp.o main.cpp
checking for flags (-MMD -MF) ... ok
> gcc "-MMD" "-MF" "/dev/null" "-m64"
checking for flags (-fdiagnostics-color=always) ... ok
> gcc "-fdiagnostics-color=always" "-m64"
checking for flags (-Wno-gnu-line-marker -Werror) ... ok
> gcc "-Wno-gnu-line-marker" "-Werror" "-m64"
checking for g++ ... /nix/store/67x7pknz0qa2j16x02idf0x98lpcspah-gcc-wrapper-14.3.0/bin/g++
checking for the linker (ld) ... g++
checking for /nix/store/67x7pknz0qa2j16x02idf0x98lpcspah-gcc-wrapper-14.3.0/bin/g++ ... ok
checking for flags (-fPIC) ... ok
> g++ "-fPIC" "-m64" "-m64"
[ 47%]: linking.debug main
/nix/store/67x7pknz0qa2j16x02idf0x98lpcspah-gcc-wrapper-14.3.0/bin/g++ -o build/linux/x86_64/debug/main build/.objs/main/linux/x86_64/debug/main.cpp.o -m64

build cache stats:
cache directory: /home/banan/repos/playground/build/.build_cache
cache hit rate: 100%
cache hit: 1
cache hit total time: 0.000s
cache miss: 0
cache miss total time: 0.000s
new cached files: 0
remote cache hit: 0
remote new cached files: 0
preprocess failed: 0
compile fallback count: 0
compile total time: 0.000s

hello

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions