C: LINK.EXE 从 Makefile 失败,但从命令行失败

发布于 2024-10-08 09:17:47 字数 2875 浏览 5 评论 0原文

当我尝试从 makefile 链接时,出现以下错误:

LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'.

Makefile Execution:

C:\Users\snmcdonald\Desktop\winMake2\winMake2>nmake "_DEBUG=" /f win2.mk build

Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

        cl /c /ZI /Fo"Debug\\" /Fe"Debug\\" main.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

main.c
        cl /c /ZI /Fo"Debug\\" /Fe"Debug\\" lib.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

lib.c
        lib Debug\lib.obj /out:Debug\lib.lib
Microsoft (R) Library Manager Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

        link Debug\main.obj Debug\lib.lib /out:Debug\main.exe
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

main.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specif
ication
LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 10.0\VC\BI
N\link.EXE"' : return code '0x450'
Stop.

但是,如果我重新运行失败的完全相同的行并从控制台链接,我会获得成功的构建。我使用的 libobj 与我的 make 文件生成的完全相同。

控制台执行:

C:\Users\snmcdonald\Desktop\winMake2\winMake2>link Debug\main.obj Debug\lib.lib /o
ut:Debug\main.exe
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

main.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specif
ication

C:\Users\SHANEM~1\Desktop\winMake2\winMake2>debug\main.exe
print from lib

我已包含我的 makefile 以供参考。

Makefile

!ifdef _DEBUG
CC = cl
CFLAGS = /c /ZI
FILES = *.c 
OUT = /Fo"Debug\\" /Fe"Debug\\"
LINKOUT = /out:Debug
DIR = Debug
!else
CC = cl
CFLAGS = /O2
FILES = *.c 
OUT = /Fo"Release\\" /Fe"Release\\"
LINKOUT = /out:Release
DIR = Release
!endif

LIB = lib
LINK = link

RM = del
RMFLAGS = *.ojb *.exe 2>NUL

build: main.exe

clean:
    $(RM) $(RMFLAGS)

rebuild: clean build

main.exe: main.obj lib.lib
    $(LINK) $(DIR)\main.obj $(DIR)\lib.lib $(LINKOUT)\main.exe

lib.lib: lib.obj
    $(LIB) $(DIR)\lib.obj $(LINKOUT)\lib.lib

main.obj: 
    $(CC) $(CFLAGS) $(OUT) main.c

lib.obj:
    $(CC) $(CFLAGS) $(OUT) lib.c

测试

我已经在 Visual C 版本 9 和版本 10 上对此进行了测试。我很困惑为什么它在我的 makefile 上失败,但在命令行手动输入时运行成功。

解决方案:

nmake /E /f win2.mk build

/E - 使用环境路径覆盖宏变量。

When I attempt to link from a makefile I get the following error:

LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'.

Makefile Execution:

C:\Users\snmcdonald\Desktop\winMake2\winMake2>nmake "_DEBUG=" /f win2.mk build

Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

        cl /c /ZI /Fo"Debug\\" /Fe"Debug\\" main.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

main.c
        cl /c /ZI /Fo"Debug\\" /Fe"Debug\\" lib.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

lib.c
        lib Debug\lib.obj /out:Debug\lib.lib
Microsoft (R) Library Manager Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

        link Debug\main.obj Debug\lib.lib /out:Debug\main.exe
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

main.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specif
ication
LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 10.0\VC\BI
N\link.EXE"' : return code '0x450'
Stop.

However, if I rerun the exact same line that failed and link from the console I get a successful build. I am using the exact same lib and obj that were produced from my make file.

Console Execution:

C:\Users\snmcdonald\Desktop\winMake2\winMake2>link Debug\main.obj Debug\lib.lib /o
ut:Debug\main.exe
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

main.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specif
ication

C:\Users\SHANEM~1\Desktop\winMake2\winMake2>debug\main.exe
print from lib

I have included my makefile for reference.

Makefile

!ifdef _DEBUG
CC = cl
CFLAGS = /c /ZI
FILES = *.c 
OUT = /Fo"Debug\\" /Fe"Debug\\"
LINKOUT = /out:Debug
DIR = Debug
!else
CC = cl
CFLAGS = /O2
FILES = *.c 
OUT = /Fo"Release\\" /Fe"Release\\"
LINKOUT = /out:Release
DIR = Release
!endif

LIB = lib
LINK = link

RM = del
RMFLAGS = *.ojb *.exe 2>NUL

build: main.exe

clean:
    $(RM) $(RMFLAGS)

rebuild: clean build

main.exe: main.obj lib.lib
    $(LINK) $(DIR)\main.obj $(DIR)\lib.lib $(LINKOUT)\main.exe

lib.lib: lib.obj
    $(LIB) $(DIR)\lib.obj $(LINKOUT)\lib.lib

main.obj: 
    $(CC) $(CFLAGS) $(OUT) main.c

lib.obj:
    $(CC) $(CFLAGS) $(OUT) lib.c

Testing

I have tested this on both Visual C version 9 and version 10. I am confused why it would fail on my makefile but run successfully when manually entered on the command line.

Solution:

nmake /E /f win2.mk build

/E - overrides macro vars with environmental paths.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光病人 2024-10-15 09:17:47

LIB = 库

这搞乱了 LIB 环境变量。是的,/E 会修复它,但您的下一个实际需要 lib.exe 的项目将会失败。选择另一个名称,win32.mak 使用“implib”。

LIB = lib

That screws up the LIB environment variable. Yes, /E will fix it but your next project that actually needs lib.exe is going to fail. Pick another name, win32.mak uses "implib".

吃素的狼 2024-10-15 09:17:47

该文件应该存在于
...\Microsoft Visual Studio 8\VC\lib

可能是环境变量设置不同。从命令行手动运行时检查环境变量设置是什么。

http://us. Generation-nt .com/answer/lnk1104-open-file-libcmt-lib-help-21575202.html

LIB 环境变量应该
包含您的各种库的路径
目录。您还可以运行
VCVARS32.BAT 文件,该文件将
自动设置环境
为你。如果你执行很多命令
线路构建,我建议创建一个
调用上面的快捷方式
提到VSVARS32.BAT

The file should exist in
...\Microsoft Visual Studio 8\VC\lib

It could be difference of environment variables setting. Check what are the enviroment variables setting when you run it manually from the command line.

http://us.generation-nt.com/answer/lnk1104-open-file-libcmt-lib-help-21575202.html

The LIB environment variable should
contain the path to your various lib
directories. You can also run the
VCVARS32.BAT file, which will
automatically set the environment up
for you. If you do a lot of command
line builds, I recommend creating a
shortcut that invokes the above
mentioned VSVARS32.BAT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文