How do you run cl.exe from cmd using the same settings as in MSVS? - c++

I have a c++ project in MSVS 2010 Express. I have been planning to write several unit tests to validate this project. Right now they go along the following lines:
#include "header.h" //Header is the header for the source I want to test
void testSomeFunction()
{
//Call function (from external src, prototype in header.h)
//Save output to file
}
int main()
{
testSomeFunction();
return 0;
}
I am creating these source files outside my project because I want to be able to run each of them as individual executable, but I am having trouble getting the Microsoft linker to link them.
This is my problem so far (CMD output):
cl ut_Converter.cpp Converter.obj
ut_Converter.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:ut_Converter.exe
ut_Converter.obj
Converter.obj
Converter.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in ut_Converter.obj
Converter.obj : fatal error LNK1313: pure module detected; cannot link with ijw/native modules
I never really use Microsoft products, I'm familiar with using the GNU tools GCC and make but I have to use the Microsoft tools for this and I have no idea how they work.
Maybe I'm going about building these tests the stupid way but it's the best way I can think of right now.
This is what I want to do:
Compile the source files in my project into object files
Compile my test files into object files
Link the test object file with the appropriate project object files to produce the test executable
How do I go about doing that? (I'm guessing there are some settings I need to set to make everything compatible but I have no idea what they are or how I would go about setting them)
Extra: I know it mentions the debug level but I'd be willing to bet that there will be other incompatible settings. Is there a way to find out what the settings are in the program so I can pass them to cl.exe when I run it?
Edit: When I say command prompt I do mean the one that comes with Visual Studio with all the environment variables setup.

Have you tried going to Programs / Microsoft Visual ... / ... Tools / ... Command Prompt, and running from that dos console window which has the environment variables setup?

Related

Error LNK2019 in a Makefile configuration type using a library

I was working on a tutorial project I found on another website where it explains how to use Java inside a C++ project (this is just for context, isn't the important part). The project was working as it should. After that I wanted to copy the code to another project, but this time it was of Makefile config type. First I ran nmake command thinking that it might tell me where the file is, but I got an error instead: NMAKE : fatal error U1064: MAKEFILE not found and no target specified, which I thought that there isn't one, and I have to make it. So I made a file and named it Makefile and inside I added only one line JNILIB=C:\Program Files\Java\jdk1.8.0_333\lib\jvm.lib that I thought I need. After that I ran nmake again, only this time I got back:
Microsoft (R) Program Maintenance Utility Version 14.29.30141.0
Copyright (C) Microsoft Corporation. All rights reserved.
But the problem still persists and that is Error LNK2019 unresolved external symbol. This means I'm doing something wrong, but to be honest I don't know how to use Makefile which clearly shows (maybe I need to add more to the file, or what I wrote is wrong).
Can anyone help me get past this error? That is the only problem I have, how to tell it that I'm using that library from that location.
I am not familiar with using nmake. Below is a test I did, putting the DLL in the same directory as cpp.
makefile:
foo: main.cpp
cl main.cpp TestDll.lib

C++20 compiling modules with Visual Studio: doesn't compile or import ixx files

Visual Studio 2019 doesn't attempt to compile my .cxx or .ixx files. Here is my .cxx file:
export module greetings;
import std.core;
export std::string get_greeting_text()
{
return "Hello, World!";
}
and here is main:
import std.core;
import greetings;
int main()
{
std::cout << get_greeting_text() << '\n';
}
I do have these flags set: /std:c++latest, /experimental:module. Error messages are
C:\...\main.cpp(2,17):error C2230: could not find module 'greetings'
C:\...\main.cpp(6,2): error C3861: 'get_greeting_text': identifier not found
...but I don't see any line about trying to compile greetings.cxx, so that's got to be the problem. Changing it to .ixx has no effect. What's the fix?
Solution:
Add greeting.ixx to Header Files. (It won't work to add it to Source Files.)
Right-click properties on greeting.ixx, and
Set Item Type to C/C++ Compiler
Set Excluded from Build to No.
Save
Build
It seems a little flaky. Rebuild failed unless I did a Clean first.
Module declaration export module greetings; is not yet working on Visual studio 2019.
You may try to add the following compiler opinions for your greetings.cxx file:
/module:export /module:name greetings /module:wrapper greetings.h /module:output greetings.ifc -c greetings.cxx
Another solution, rename greetings.cxx to greetings.ixx. The .ixx extension is required for module interface files in Visual Studio.
In order to use a different file extension with MSVC so you can use intellisense in Visual Studio Code, there is actually a work-around.
With the cl.exe command, there is an '/interface' option. You can do like so:
In My cl.exe CommandFile:
/interface /Tp UI/ApplicationHost.cpp
/reference ApplicationHost=Build\Debug\ApplicationHost.ifc
UI/WorldEngine.cpp
/link /SUBSYSTEM:WINDOWS
Build/Debug/pch.obj
/Interface - Let's the compiler know there is an ifc module/header coming up.
/Tp - Forces the compiler to recognize it as a C++ file, useful if renaming to .cppm.
/reference - Tells the compiler where to get the dependency information for the following source code.

C++ Error RC2104 trying to compile PuTTY-PSCP (for Windows) on Visual Studio 6.0

I was about to use PuTTY Development source code for Windows to create my own client application (found here: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) but as I tried to compile the PSCP project (SCP Client), I got the following error :
C:\work\2015\Putty\windows\version.rc2 (18): error RC2104 : undefined
keyword or key name: BINARY_VERSION
I've been going through the various posts involving this error but didn't find anything working :
error RC2104: undefined keyword or key name: DS_SETFONT :
On this post I noticed that the version of MSVC was brought up so I figured maybe something has to be done to get PuTTY to work on VC 6.0 ?
Also I tried to add #include <windows.h> in both version.rc2 (version.rc2 is used for inclusion in all .rc files) and pscp.rc, none worked.
I'll be quick to answer if you need any information (project properties, source code...)
USING Visual Studio 6.0 with SP6 on Windows 8.1
Probably, wrong version.h is seen.
Correctly, the file version.h in the project folder should be seen.
Please try to modify version.rc2:
#include "version.h"
to
#include "..\\..\\..\\version.h"
At least, resource compiler will end successfully.
If you search through the PuTTY source files, you'll notice that BINARY_VERSION is defined in version.h and used in windows/version.rc2, which #includes version.h.
Since your version.rc2 isn't seeing version.h, try to figure out why: Is version.h still present and does it still contain BINARY_VERSION? Are your include paths correct? Is there another version.h somewhere else in your include path that's getting picked up by mistake?
Which source code are you using ?
I tested latest(0.64) "Release source code for Windows".
direct link is
http://the.earth.li/~sgtatham/putty/latest/putty-src.zip
I had tried to compile using VC++ 6.0 Professional with SP6, on my PC,
running Windows XP SP3.
After extracting putty-src.zip to somewhere with keeping folder
structures, did you correctly opened 'putty.dsw' in "putty-src\windows\MSVC" folder?
You should be find in 7 projects in 'FileView' tab of the workspace
in Visual Studio 6.0.
You can switch active project to 'pscp' with context menu via
right button click on 'pscp' project.
With modified version.rc2, resource compiler finished successful.
But two (sshshare.c, winsftp.c) C source files failed compiling
with 20 errors. in 'pscp' project.
Errors while compiling 'winsftp.c' is caused 'TIME_POSIX_TO_WIN'
and 'TIME_WIN_TO_POSIX' macros.
'ull(unsigned long long)' is a 64-bit integer-suffix, newly defined in C99. Since C99 standard is not support on VC6, then caused errors.
I had temporally modified
11644473600ull ------> ((ULONGLONG)11644473600)
10000000ull ---------> ((ULONGLONG)10000000)
and confirmed errors are cleared. (Sorry, no validation the code is correctly generated)
3 errors while compiling 'sshshare.c' is also caused another macro.
I cannot understand why you got 116 errors.

CRT library: wrong version

During the compilation process of my application (Debug mode), I am getting the following error, related to the CRT library:
16>libcpmt.lib(stdhndlr.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in poStat.obj
According to the Microsoft MSDN topic, I need to set the runtime library to /MTd in order to append the correct library compiled with debug mode. I also have to define a _DEBUG flag - and thats exactly what I have done.
Even though I have did everything it requires to use the CRT with debug mode, its still trying to use one without the debug mode (libcpmt.lib instead of the libcpmtd.lib).
How can I fix this?
Update
When I go to the C:\Program Files\Microsoft Visual Studio 11.0\VC\lib and change the name of libcpmtd.lib to the libcpmt.lib (had to temporary remove the existing libcpmt.lib) it builds successfully in a debug mode.
As Hans Passant already pointed out, the reason you have this is because some of the .lib or .obj files you are linking were compiled with Release settings, and some with Debug. The real question for you now is how to find which libraries or object files need to be fixed. Here is one way to do this
link /dump /all "Path_To_Lib_or_Obj" | findstr /L "\/DEFAULTLIB"
This will print out all default libraries, including CRT. This command has to be executed for each .lib and .obj that appears on your linker build command. For Debug you should see something like:
/DEFAULTLIB:msvcprtd
/DEFAULTLIB:MSVCRTD
/DEFAULTLIB:OLDNAMES
...
and for Release:
/DEFAULTLIB:msvcprt
/DEFAULTLIB:MSVCRT
/DEFAULTLIB:OLDNAMES
...

Can not find mpirxx.h after building the mpir-2.6.0 library in vc++

I need to use mpir-2.6.0 library with visual c++ 2010. My code is going to be in c++.
I extracted both folders (mpir-2.6.0 and vsyasm-1.2.0-win32). Then, I copied the content of the folder vsyasm-1.2.0-win32 (including vsyasm.exe after renaming it to yasm.exe and placing it in: C:\Program Files\Microsoft Visual Studio 11.0\VC\bin\. Then, I opened the project: mpir.sln which is placed in: \mpir-2.6.0\build.vc10. Then, I changed the Project configuration to: Release.
When I try to build the whole solution, I get a lot of errors such as:
error C1020: unexpected #endif c:\proj\mpir-2.6.0\mpir-2.6.0\mpir.h 4 1 lib_mpir_gc
error C1020: unexpected #endif c:\proj\mpir-2.6.0\mpir-2.6.0\mpir.h 4 1 lib_mpir_gc
error C1020: unexpected #endif c:\proj\mpir-2.6.0\mpir-2.6.0\mpir.h 4 1 lib_mpir_gc
But when I build every library separately, it is successful.
After the build, I went to: \mpir-2.6.0\build.vc10\Win32\Release
and find the files:
mpir.lib
mpirxx.lib
mpir.pdb
mpirxx.pdb
But I can not find the files:
mpir.h
mpirxx.h
Which I need to copy them the visual studio include file.
My main source for these configuration is: http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/ (but this was for old versions for the library and the ysam). I do not understand the reasons for these errors. I was able to configure the library with old version. But I need to upgrade as there are improvements in the library and mine is very old one.
The output files are now located in mpir\lib\Win32\Release or mpir\dll\Win32\Release.
The recommended way to build MPIR specifcally for your CPU is to first run mpir_config.py which is located in the build.vc10 directory. mpir_config.py will prompt you with a list of CPU options. Then when you open mpir.sln, you should only compile code for the specific CPU. And then you compile the C++ wrapper (i.e. compile lib_mpir_p3 first, and then lib_mpir_cxx).
Edit: I just noticed another error from your original post. You need to rename the directory "mpir-2.6.0" to "mpir". The directory layout needs to be "<>\mpir\build.vc10"
I had the same problem with MPIR 2.6.0 and VS 2012. What I did to make MPIR work was to build lib_gc and lib_cxx, both with win32 and release mode. Because my c++ application is win32 console, this way it worked perfectly.
Becuase my windows is 64, I thought I had to build 64 versions, but turned out that I was wrong.