xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
9.85k stars 774 forks source link

在windows下编译使用xmake f -a x64出错 #41

Closed xiren7 closed 7 years ago

xiren7 commented 7 years ago

编译环境: windows 10, msvc 2015 update 3

执行以下命令后: xmake f -a x64 xmake

出错信息如下: error: build.objs\test1\src\main.cpp.obj : fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

原因应该是link用的是x86版本的stdlib而不是x64的, 我看了下在项目目录的.xmake/xmake.conf文件里面的["__vsenv_lib"]的值的是x86的, 并没有更新成x64的, 也就是说执行"xmake f -a x64"命令后, 并没用通过执行"vcvarsall.bat x64"来更新["__vsenv_lib"]的值.

我尝试修改了下xmake安装目录下的platforms/windows/checker.lua文件, 不过问题并没有完全解决:

function _check_vs(config)

        -- checked?
        -- 我目前暂时注释掉了这几行, 但是会使每次编译时都要重新checking一遍
        -- if config.get("vs") and config.get("__vsenv_path") then
        --    return
        -- end

        ... 
        if vs then

            -- get the envname
            local envname = version2envname[vs]

            -- clear vs first
            vs = nil

            -- attempt to check it
            if envname then
                local envalue = os.getenv(envname)
                if envalue and _apply_vs(config, envalue) then
                    vs = version  -- !!! 这里的version并没有定义过 !!!
                end
            end
        end
waruqi commented 7 years ago

你可以尝试加上-c参数,重新检测下:xmake f -a x64 -c

如果还不行,可以把 检测信息贴下,或者加上 -v 后,贴下更加详细的检测信息给我

xiren7 commented 7 years ago

用的xmake的版本是最新的, 错误依旧是:

main.cpp.obj : fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

详细信息如下:

D:\cpp\test2>xmake create -l c++ -t 1 test2
create test2 ...
create ok!

D:\cpp\test2>xmake f -a x64 -c
checking for the c compiler (cc) ... cl.exe
checking for the c++ compiler (cxx) ... cl.exe
checking for the linker (ld) ... link.exe
checking for the static library archiver (ar) ... link.exe -lib
checking for the shared library linker (sh) ... link.exe -dll
checking for the static library extractor (ex) ... lib.exe
checking for the assember (as) ... no
checking for the debugger ... vsjitdebugger.exe
configure
{
    ex = "lib.exe"
,   ccache = true
,   host = "windows"
,   ar = "link.exe -lib"
,   buildir = "build"
,   plat = "windows"
,   ld = "link.exe"
,   cxx = "cl.exe"
,   make = "make"
,   mode = "release"
,   cc = "cl.exe"
,   dd = "C:\\windows\\system32\\vsjitdebugger.exe"
,   kind = "static"
,   sh = "link.exe -dll"
,   vs = "2015"
,   clean = true
,   arch = "x64"
}
configure ok!

D:\cpp\test2>xmake -v
configure
{
    ex = "lib.exe"
,   ccache = true
,   host = "windows"
,   ar = "link.exe -lib"
,   buildir = "build"
,   plat = "windows"
,   ld = "link.exe"
,   sh = "link.exe -dll"
,   make = "make"
,   mode = "release"
,   cc = "cl.exe"
,   dd = "C:\\windows\\system32\\vsjitdebugger.exe"
,   kind = "static"
,   cxx = "cl.exe"
,   vs = "2015"
,   clean = true
,   arch = "x64"
}
configure ok!
clean ok!
[00%]: ccache compiling.release src\main.cpp
cl.exe -c -Ot -nologo -Gd -MP4 -D_MBCS -D_CRT_SECURE_NO_WARNINGS -Fobuild\.objs\
test2\src\main.cpp.obj src\main.cpp
[100%]: linking.release test2.exe
link.exe -nologo -dynamicbase -nxcompat -machine:x64 -out:build\test2.exe build\
.objs\test2\src\main.cpp.obj
error: C:\Program Files (x86)\xmake/core/base/os.lua:457: build\.objs\test2\src\
main.cpp.obj : fatal error LNK1112: module machine type 'X86' conflicts with tar
get machine type 'x64'

stack traceback:
    [C]: in function 'error'
    [C:\Program Files (x86)\xmake/core/base/os.lua:457]: in function 'raise'
    [C:\Program Files (x86)\xmake/core/sandbox/modules/os.lua:377]: in function
'raise'
    [...)\xmake\core\sandbox\modules\import\core\tool\linker.lua:69]: in functio
n 'link'
    [C:\Program Files (x86)\xmake\actions\build\kinds\binary.lua:67]: in functio
n 'build'
    [C:\Program Files (x86)\xmake\actions\build\builder.lua:35]: in function 'sc
ript'
    [C:\Program Files (x86)\xmake\actions\build\builder.lua:53]: in function '_b
uild_target'
    [C:\Program Files (x86)\xmake\actions\build\builder.lua:75]: in function '_b
uild_target_and_deps'
    [C:\Program Files (x86)\xmake\actions\build\builder.lua:144]: in function 'b
uild'
    [C:\Program Files (x86)\xmake\actions\build\main.lua:67]:
xiren7 commented 7 years ago

.xmake/cache/config:

{
    ["options_all"] = 
    {
        ["clean"] = true
    ,   ["arch"] = "x64"
    }

,   ["mtimes"] = 
    {
        ["xmake.lua"] = 1477712949
    }

,   ["__version"] = "XMake v2.0.5"
,   ["options_test2"] = 
    {
        ["clean"] = true
    ,   ["arch"] = "x64"
    }
}

.xmake/xmake.conf:

{
    ["_TARGETS"] = 
    {
        ["test2"] = 
        {
            ["ex"] = "lib.exe"
        ,   ["ccache"] = true
        ,   ["__vsenv_vsinstalldir"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\"
        ,   ["host"] = "windows"
        ,   ["ar"] = "link.exe -lib"
        ,   ["__vsenv_vcinstalldir"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\"
        ,   ["arch"] = "x64"
        ,   ["clean"] = true
        ,   ["buildir"] = "build"
        ,   ["__ccache"] = "ccache"
        ,   ["__vsenv_include"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\INCLUDE;C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.10150.0\\ucrt;C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6\\include\\um;C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\shared;C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\um;C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\winrt;"
        ,   ["cxx"] = "cl.exe"
        ,   ["__vsenv_devenvdir"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\"
        ,   ["__vsenv_libpath"] = "C:\\windows\\Microsoft.NET\\Framework\\v4.0.30319;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\LIB;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\LIB;C:\\Program Files (x86)\\Windows Kits\\8.1\\References\\CommonConfiguration\\Neutral;\\Microsoft.VCLibs\\14.0\\References\\CommonConfiguration\\neutral;"
        ,   ["__vsenv_path"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow;C:\\Program Files (x86)\\MSBuild\\14.0\\bin;C:\\Program Files (x86)\\MSBuild\\14.0\\bin;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools;C:\\windows\\Microsoft.NET\\Framework\\v4.0.30319;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\VCPackages;C:\\Program Files (x86)\\HTML Help Workshop;C:\\Program Files (x86)\\HTML Help Workshop;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Team Tools\\Performance Tools;C:\\Program Files (x86)\\Windows Kits\\8.1\\bin\\x86;C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6 Tools\\;C:\\ProgramData\\Oracle\\Java\\javapath;D:\\dev\\Cppcheck;D:\\oracle\\product\\11.2.0\\dbhome_2\\bin;d:\\dev\\Ruby22\\bin;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Google\\Cloud SDK\\google-cloud-sdk\\bin;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit\\;D:\\Meld;C:\\Program Files (x86)\\Cppcheck;D:\\dev\\Go\\bin;d:\\dev\\CMake\\bin;d:\\dev\\Git\\cmd;d:\\dev\\Git\\mingw64\\bin;d:\\dev\\Git\\usr\\bin;C:\\Program Files (x86)\\Windows Phone TShell\\;D:\\dev\\nodejs\\;C:\\Users\\Administrator.PG-20150115CDAK\\AppData\\Roaming\\npm;D:\\dev\\xmake;C:\\Users\\Administrator.PG-20150115CDAK\\AppData\\Roaming\\npm"
        ,   ["mode"] = "release"
        ,   ["__vsenv_lib"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\LIB;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\LIB;C:\\Program Files (x86)\\Windows Kits\\10\\lib\\10.0.10150.0\\ucrt\\x86;C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6\\lib\\um\\x86;C:\\Program Files (x86)\\Windows Kits\\8.1\\lib\\winv6.3\\um\\x86;"
        ,   ["make"] = "make"
        ,   ["cc"] = "cl.exe"
        ,   ["dd"] = "C:\\windows\\system32\\vsjitdebugger.exe"
        ,   ["kind"] = "static"
        ,   ["plat"] = "windows"
        ,   ["vs"] = "2015"
        ,   ["ld"] = "link.exe"
        ,   ["sh"] = "link.exe -dll"
        }

    ,   ["all"] = 
        {
            ["ex"] = "lib.exe"
        ,   ["ccache"] = true
        ,   ["__vsenv_vsinstalldir"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\"
        ,   ["host"] = "windows"
        ,   ["ar"] = "link.exe -lib"
        ,   ["__vsenv_libpath"] = "C:\\windows\\Microsoft.NET\\Framework\\v4.0.30319;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\LIB;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\LIB;C:\\Program Files (x86)\\Windows Kits\\8.1\\References\\CommonConfiguration\\Neutral;\\Microsoft.VCLibs\\14.0\\References\\CommonConfiguration\\neutral;"
        ,   ["sh"] = "link.exe -dll"
        ,   ["ld"] = "link.exe"
        ,   ["__vsenv_devenvdir"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\"
        ,   ["__ccache"] = "ccache"
        ,   ["arch"] = "x64"
        ,   ["plat"] = "windows"
        ,   ["__vsenv_include"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\INCLUDE;C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.10150.0\\ucrt;C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6\\include\\um;C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\shared;C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\um;C:\\Program Files (x86)\\Windows Kits\\8.1\\include\\winrt;"
        ,   ["buildir"] = "build"
        ,   ["cc"] = "cl.exe"
        ,   ["mode"] = "release"
        ,   ["__vsenv_lib"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\LIB;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\ATLMFC\\LIB;C:\\Program Files (x86)\\Windows Kits\\10\\lib\\10.0.10150.0\\ucrt\\x86;C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6\\lib\\um\\x86;C:\\Program Files (x86)\\Windows Kits\\8.1\\lib\\winv6.3\\um\\x86;"
        ,   ["cxx"] = "cl.exe"
        ,   ["make"] = "make"
        ,   ["dd"] = "C:\\windows\\system32\\vsjitdebugger.exe"
        ,   ["kind"] = "static"
        ,   ["__vsenv_vcinstalldir"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\"
        ,   ["vs"] = "2015"
        ,   ["clean"] = true
        ,   ["__vsenv_path"] = "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow;C:\\Program Files (x86)\\MSBuild\\14.0\\bin;C:\\Program Files (x86)\\MSBuild\\14.0\\bin;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools;C:\\windows\\Microsoft.NET\\Framework\\v4.0.30319;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\VCPackages;C:\\Program Files (x86)\\HTML Help Workshop;C:\\Program Files (x86)\\HTML Help Workshop;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Team Tools\\Performance Tools;C:\\Program Files (x86)\\Windows Kits\\8.1\\bin\\x86;C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6 Tools\\;C:\\ProgramData\\Oracle\\Java\\javapath;D:\\dev\\Cppcheck;D:\\oracle\\product\\11.2.0\\dbhome_2\\bin;d:\\dev\\Ruby22\\bin;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Google\\Cloud SDK\\google-cloud-sdk\\bin;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit\\;D:\\Meld;C:\\Program Files (x86)\\Cppcheck;D:\\dev\\Go\\bin;d:\\dev\\CMake\\bin;d:\\dev\\Git\\cmd;d:\\dev\\Git\\mingw64\\bin;d:\\dev\\Git\\usr\\bin;C:\\Program Files (x86)\\Windows Phone TShell\\;D:\\dev\\nodejs\\;C:\\Users\\Administrator.PG-20150115CDAK\\AppData\\Roaming\\npm;D:\\dev\\xmake;C:\\Users\\Administrator.PG-20150115CDAK\\AppData\\Roaming\\npm"
        }

    }

,   ["__version"] = "XMake v2.0.5"
}

["__vsenv_lib"] = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\LIB;C:\Program Files (x86)\Windows Kits\10\lib\10.0.10150.0\ucrt**x86;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6\lib\um\x86;C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x86**;"

这个明显不对, 用的是x86的lib, 而不是x64的; x64的应该是这样的:

["__vsenv_lib"] = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB**amd64;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\LIB\amd64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.14393.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.14393.0\um\x64**;"

waruqi commented 7 years ago

正常情况,检测结果应该是这样的:

checking for the Microsoft Visual Studio version ... 2013
checking for the c compiler (cc) ... cl.exe
checking for the c++ compiler (cxx) ... cl.exe
checking for the linker (ld) ... link.exe
checking for the static library archiver (ar) ... link.exe -lib
checking for the shared library linker (sh) ... link.exe -dll
checking for the static library extractor (ex) ... lib.exe
checking for the assember (as) ... ml64.exe
checking for the debugger ... vsjitdebugger.exe
configure

如果看到第一行有检测到 vs版本,已经 as 为 ml64.exe ,说明检测x64成功了,但是你这貌似这两个没去再次重新检测,可能是读到cache里面去了。。

因为目前只有重新检测vs后,才会去更新arch对应的环境变量,按理加上-c后,会去重新检测,不走cache的。。 = =

你尝试执行 xmake clean --all 清掉所有config的cache,再试下,或者手动删下 .xmake 目录。。 去强制重新检测vs版本。。

xmake clean --all
xmake f -a x64 -c
xiren7 commented 7 years ago

搞清楚了, 原来是global cache的原因, 我把C:\ProgramData.xmake里面的文件删除了就正常了.

还有几个问题请教下:

  1. 有专门清除global cache的命令吗? 试了下xmake g -c没用
  2. 还有就是xmake f过后这里, kind显示的是static, 但是配置文件设置的kind是binary, 这个是显示出错了吗?
  3. 在platforms/windows/checker.lua文件141行, 这里的"version"应该没有定义初始化过就用来赋值了, 这个应该是bug?
function _check_vs(config)

        if vs then

            -- get the envname
            local envname = version2envname[vs]

            -- clear vs first
            vs = nil

            -- attempt to check it
            if envname then
                local envalue = os.getenv(envname)
                if envalue and _apply_vs(config, envalue) then
                    vs = version   <----- !!!!!!! 
                end
            end
        end
waruqi commented 7 years ago
  1. 这个我之后加下
  2. 配置里面的kind,仅影响xmake.lua中 set_kind("$(kind)") 这种,动态切换类型的设置(一般用于static/shared的切换编译),例如:xmake f -k shared,如果set_kind里面写死的binary,执行xmake f 是无法影响到的

3 是bug,windows检测这块,我这两天优化下检测逻辑,会一并修复这些问题。。

xiren7 commented 7 years ago

恩, 只用kind表示可能有些歧义. 实际上kind分为library, binary, 然后library type分为static, shared.

waruqi commented 7 years ago

不是 kind统一表示为 binary二进制可执行程序 static静态库 shared动态库,这些都是生成目标文件的类型

以后还会扩展其他类型 例如 application 等等

你可以在xmake.lua 通过set_kind("static")写死指定

也可以通过 xmake f -k xxx 进行设置 set_kind("$(kind)")或者把 "$(kind)" 用于其他地方,说白了就是配置参数的设置 和动态获取

waruqi commented 7 years ago

我已经修复这个bug,并且对xmake g 增加了 -c 清cache 的选项。。

xiren7 commented 7 years ago

感谢, ^_^