20

Please help, I have spent all day trying to make my c++ app compile.

My project contains one source file: Foo.cpp. Here is its code:

#include <jni.h>
#include <com_Foo.h>

JNIEXPORT jint JNICALL Java_com_Foo_add
(JNIEnv * env, jobject obj, jint x, jint y)
{
    return x+y;
}

as you can see I was trying to use JNI, but I don't think that has anything to do with the problem. Here is what I tried on the command line:

C:\Users\michael\cworkspace\foo>cl -LD Foo.cpp -FeFoo.dll
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Foo.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/dll
/implib:Foo.lib
/out:Foo.dll
Foo.obj
LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'

In my environment I have set:

INCLUDE: C:_\include;C:\frankenstein\includes;C:\Program Files\Java\jdk1.6.0_24\include;C:\Program Files\Java\jdk1.6.0_24\include\win32;C:_\include\win32;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include

LIBPATH: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib

I checked in the lib directory, it contains a "libcmt.lib" file. Don't know if it is case sensitive, but I know that I never changed anything in the visual studio files or directories.

Would somebody please tell me how I can make this work?

3
  • BTW, this file exists on my drive: C:_\include\com_Foo.h, the dependency on libcmt must come indirectly from the jni.h include.
    – msknapp
    Feb 20, 2012 at 4:52
  • I think this might stem from not creating the code in visual studio, but trying to use its compiler anyways. I created the application in Code::Blocks, and when I could not make the MinGW32 g++ compiler work, I tried using the visual studio compiler. Perhaps it assumes a certain project configuration. Is it possible that the visual studio compiler only works when the project was built in visual studio?
    – msknapp
    Feb 20, 2012 at 5:11
  • This is perhaps quite an old thread, but since I ran into it now, I thought it might be useful. @mrsheen's answer below worked for me, but I had to run the program from the developer cmd rather than the normal (where the error persisted). Just an FYI for someone who might still be looking for this out there. Dec 22, 2016 at 18:21

8 Answers 8

26

Resurrecting this thread in late 2018 since none of the other solutions helped my case :)

For me this error started appearing out of nowhere after i installed Windows Driver Kit for a completely unrelated project.

If you don't need "Spectre Mitigation" (you most likely do not) and seeing this error it's highly probable that you just have to set Project Properties (property pages) -> Configuration Properties -> C/C++ -> Code Generation -> Spectre Mitigation to Disabled.

Magically it was set to Enabled by the installer of Visual Studio tools for WDK in all projects even tho they got nothing to do with WDK nor the Platform toolset was selected for any driver related stuff. Microsoft at its finest.

6
  • 3
    Great, thanks. it works, I wonder how did you get to that conclusion? :) Mar 12, 2019 at 18:47
  • @CEOatApartico i did not.. it was more like trial and error. I think i have spent days with this back then trying things out and suddenly i noticed something that i have not seen there before. so i tried to disable it :) Aug 22, 2019 at 14:59
  • 1
    it was less painless for us :) Aug 31, 2019 at 17:49
  • 1
    This should be marked as answer, saved my day, thank you very much!
    – n0ne
    Dec 17, 2019 at 8:41
  • @nOne glad it worked for you. I think my answer is to a newer problem than that the OP had :) either way it's good to see it helps people. Dec 21, 2019 at 15:28
13

I suspect the command prompt you are using to run the compiler does not have all the required environment variables defined.

To make sure the command line environment is correctly configured make sure you have run the vsvars32.bat batch file.

2
  • This does not fix the problem. On my system, cl doesn't work unless I run vsvars32.bat first. I still get the error though. Mar 29, 2014 at 0:34
  • Now vsvars64.bat :)
    – atakli
    Nov 25, 2022 at 14:50
9

Just to add my experience here, since it may help someone, and it applies to newer VS editions (2017 in my case).

I was getting a similar error during compiling when I moved my project to a new machine. I finally figured out that one of the projects in my dependencies had spectre mitigation turned on, and the libs with that support are not included by default:

enter image description here

1
  • Yes, this is what fixed it for mine, as well. The /QSpectre flag is enabled by default now, but it doesn't install the Spectre-mitigated libraries by default. Strange choice of defaults, but running the installer and checking this option fixed the issue for me.
    – reirab
    Feb 4, 2020 at 20:19
2

This worked for me when I got …

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

Your project name == “Project Name” for this example.

Go to the 'Solution Explorer' window for your Project.

RIGHT Click on the Project "Project Name", not the Solution "Project Name".

This will open a long menu.

At the bottom is "Properties" Click on it.

This will open the “Project Name” Property Pages window.

IF the name of the window (upper left) says...

“Solution 'Project Name' Property Pages

Close this window, you right clicked on the Solution, not the Project, you need to right click on the project name.

On the “Project Name” Property Pages

Left hand panel expand Configuration Properties

Then click on General

Now the right panel should show the 'General' Configuration Properties' for your current Project.

Look down the list to 'Platform Toolset'

If the version of VS you are using is not the one listed you needed to change it.

To change the version of VS just put you mouse pointer at the end of the current VS version name that is displayed and click.

A box with a down arrow will show at the right hand side of that row, click on it.

This will open a window with a list of VS versions.

Pick the version of VS you are NOW using.

That window should close, and your selection should have replaced the old project VS version that was used to build your project when I was first built.

Then at bottom of “Project Name” Property Pages click Apply, then OK.

Now try to build Project.

I hope this helps, it works for me when working on projects that were built with earlier versions of VS.

Good luck Mike.

2

Having just hit seemingly this same error (certainly the libcmt.lib not found) whilst trying to install the rasPi Pico SDK on Win10, solution was to run vcvars64.bat which (on my install) was located in : C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build on the command line and then rerun nmake.

1

Right click on ProjectName (within Solution Explorer) -> Properties -> Linker -> General -> Additional Library Directories and set it like this:

$(SolutionDir)$(Platform)\$(Configuration)\;$(VCToolsInstallDir)\lib\$(PlatformShortName);$(VCToolsInstallDir)atlmfc\lib\$(PlatformShortName)
3
  • How does this fix the original question? Mind going into more depth? May 13, 2019 at 20:22
  • This code set path to VC Build Tools via Visual Studio macro. It makes path code independent in comparison with hardcode like C:\Program Files\.... If toolset is installed you just need to set path to it in your project.
    – slinkin
    May 13, 2019 at 22:11
  • Thanks to MicroPC John for this answer I have solved this problem, you need install all spectre lib. Vistual Studio Installer->Modify->Component->Any spectre lib
    – slinkin
    Sep 19, 2019 at 23:59
1

Running cl.exe directly requires that environment variables are set that point to libraries and tools. To get an idea of what variables need installed and what their values should be, I recommend installing Microsoft Visual C++ Compiler for Python 2.7. It adds command prompts to the start menu which automatically load the needed environment variables.

After running the prompt, run set to see all of the environment variables.

Examples:

LIB=C:\Users\JBeach\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Lib;C:\Users\JBeach\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\WinSDK\Lib;
LIBPATH=C:\Users\JBeach\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Lib;C:\Users\JBeach\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\WinSDK\Lib;
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_ARCHITEW6432=AMD64
VCINSTALLDIR=C:\Users\JBeach\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\
WindowsSdkDir=C:\Users\JBeach\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\WinSDK\
0

I got this same error when running cl.exe from the Developer Command Prompt for VS2012 command window. Turned out all I had to do was restart the command prompt by right-clicking on it and choosing Run as administrator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.