cl error 0xc000007b when invoked from scons script - c++

I am trying to compile a simple program using scons + MSVC compiler under Windows. Program source is just simple "Hello world".
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
return 0;
}
SConstruct is utterly simple:
Program("hw.cc")
When I run scons in the source directory, I get
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
cl /Fohw.obj /c hw.cc /TP /nologo
scons: *** [hw.obj] Error 123
scons: building terminated because of errors.
in the console and pop-up message with 0xc000007b error.
Aslo results of where command:
where cl
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\bin\Hostx86\x86\cl.exe
where scons
C:\Python35-32\Scripts\scons.bat
I don't have any clue what's wrong.
UPD
SCons debug output
UPD 2

After some experiments with cl and scons I have finally figured out what was wrong and how to fix it.
First of all, cl should be available from the command line. If after entering command cl in the console you get errors like command not found, you should add path to cl.exe to the PATH system variable. In my case
PATH=<rubbish>;C:\Microsoft\VC\Tools\MSVC\14.14.26428\bin\Hostx86\x86;
After this you should set up variables INCLUDE and LIB to tell compiler and linker where to find include files and libs. And this part is a little bit tricky, because, to my surprise, cl does not compiling anything without Windows Kits 10 (whatever it is). Thus, you should specify its includes and libs accordingly. In my case
INCLUDE=C:\Microsoft\VC\Tools\MSVC\14.14.26428\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt
LIB=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17134.0\ucrt\x86;C:\Microsoft\VC\Tools\MSVC\14.14.26428\lib\x86;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17134.0\um\x86
When these variables are set up correctly, hw.cc should compile fine.
And, probably, this should do the trick for scons too, but to make one hundred percent sure it works correctly, SConstruct should be modified to something like this
import os
env = Environment(ENV = os.environ)
flags = ["/EHsc"] # Flags are completely optional
env.Program("hw.cc", CXXFLAGS=flags)
With all these steps, everything should compile fine.

Related

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.

when building pixman library, the prompt always says "can not find xxx.h(the std header"?

I'm using Windows 10 system, VS2015. I want to build pixman-0.34.0 now.
The tutorials here: http://cairographics.org/end_to_end_build_for_win32/ tells me to use these prompt lines:
cd %ROOTDIR%\pixman\pixman
sed s/-MD/-MT/ Makefile.win32 > Makefile.fixed
move /Y Makefile.fixed Makefile.win32
make -f Makefile.win32 "CFG=release"
however, the powershell(or msys64) always tells me that
"pixman-0.34.0\pixman\pixman.h(105): fatal error C1083: Cannot open include file: 'stdint.h': No such file or directory"
which is very strange because < stdint.h > does exist in my computer! If I create a new .cpp file which includes < stdint.h >, I could successfully compile it.
I tried to move all the source code from the pixman's root directory to VS's include directory...
The prompt now tells that no < time.h > file.
I guess I need to set include directory somewhere, but I dont know how to do it.
It almost drives me crazy now. I have spent a whole day trying to solve it but none of my try works.
Can anyone help me?
See also this quesiton: Does Visual Studio 2015 have float.h?
Float.h got moved for VS 2015. I was able to build pixman by updating Makefile.win32.common in the root source directory, line 28:
BASE_CFLAGS = -nologo -I. -I$(top_srcdir) -I$(top_srcdir)/pixman -I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\ucrt" -I"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include"

C++ Compile source file from Visual Studio

I need to compile a external source file from Visual Studio.
I picked that from the developer command prompt:
cl /EHsc test.cpp
So I tried this:
system("cl /EHsc test.cpp");
But the command 'cl' doesn't exist in the normal cmd that is called by system()
Any suggestions how to use the compiler function anyway?
Now I did it myself and for those ones who are curious I did it by making a copy of the vcvars32.bat and added a few commands like this:
cd %~dp0%
cl /LD source.cpp /EHsc ;remember putting this part after the bat-file called all important commands
Now I just need to execute this file from c++ and here we go.

VC12 Command Line Error when Linking .lib Files

I'm trying to compile a C++ program from the VS2013 command window with includes and linked libraries. Pretty standard stuff. However, Microsoft's website explaining the syntax is not very clear to me.
I'm trying the following command:
cl /EHsc program.cpp /I "\path\to\includes"
/LIBPATH:"\path\to\library\directory" /LINK libfile1.lib libfile2.lib
Note: It's all on one line when I execute it, but for readability I've split it here.
The quotes are because some of the directories in the paths have whitespace in them beyond my control (like Program Files).
I'm trying to adapt this command from a property sheet I made using the Visual Studio interface, so if it helps, the mapping I'm making is:
IncludePath --> /I
LibraryPath --> /LIBPATH:
AdditionalDependencies (under Link) --> /LINK
I am running this as an admin in the x64 Native Tools Command Prompt on a machine running 64-bit Windows 10. However, it gives me these warnings and errors:
cl: Command line warning D9002: ignoring unknown option '/LIBPATH:\path\to\library\directory'
cl: Command line warning D9002: ignoring unknown option '/LINK'
LINK : fatal error LNK1181: cannot open input file 'libfile1.lib'
Where am I going wrong?
The /link option must be lowercase and place the /LIBPATH option after it:
cl /EHsc program.cpp /I "\path\to\includes" /link /LIBPATH:"\path\to\library\directory" libfile1.lib libfile2.lib
try to add the lib's path to your Library Directories (Configuration Properties >> VC++ Directories).
and compile again

Strange VC Linker Error LNK1107 that references link.exe

I am getting a really strange linker error appearing:
link.exe : fatal error LNK1107: invalid or corrupt file: cannot read at 0x270
But this is strange, because usually the error message tells you what object is invalid or corrupt. In other words, typically this error message looks something like this:
myDLL.dll : fatal error LNK1107: ....
However, in this case, the invalid object is the application itself (link.exe) that is running!
I've tried replacing the executable with another copy that I know is valid. Same error.
The command I am running is this:
../vendor/microsoft/msdev80_2005/VC/bin/link.exe /NOLOGO /SUBSYSTEM:CONSOLE /LIB
PATH:../vendor/microsoft/msdev80_2005/VC/atlmfc/lib /LIBPATH:../vendor/microsoft
/msdev80_2005/VC/lib /LIBPATH:../vendor/microsoft/msdev80_2005/VC/PlatformSDK/Li
b /LIBPATH:lib/win32/dbg /OUT:bin/win32/dbg/bugshow.exe Advapi32.lib ws2_32.lib
bugshow/obj/win32/dbg/main.o libA.lib libB.lib libC.lib libD.lib
main.o is getting compiled using the following compiler directive:
../vendor/microsoft/msdev80_2005/VC/bin/cl.exe /nologo /X /w /EHsc -I../vendor/m
icrosoft/msdev80_2005/VC/atlmfc/include -I../vendor/microsoft/msdev80_2005/VC/in
clude -I../vendor/microsoft/msdev80_2005/VC/PlatformSDK/Include /D_WIN32 /DBOOST
_USE_WINDOWS_H /DWIN32_LEAN_AND_MEAN /D_WIN32_WINNT=0x0501 /DBOOST_THREAD_USE_LI
B /DBOOST_ALL_NO_LIB /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE /Z7
/MTd /c -I../vendor/boost.org/boost/1.45.0/include /Fobugshow/obj/win32/dbg/main.o
bugshow/main.cpp
Any ideas why link.exe would be failing like this?
UPDATE:
When I add the folder that link.exe resides in to my PATH, and then call link.exe without the path prefix, I get the following error instead:
LINK: fatal error LNK1181: cannot open input file 'link.exe'
Why is link.exe trying so desperately to open itself??!?!
I figured out the problem!
There is a nasty little ENVIRONMENT variable that the MSVC linker uses (described here) on Microsoft's MSDN page. It says:
LINK, if defined, prepends arguments in the command line.
My system had the environment variable LINK defined as STATIC by a recent application I installed. This caused link.exe to interpret the command-line instantiation of itself as follows:
STATIC link.exe /NOLOGO /SUBSYSTEM:CONSOLE ...
In other words, *argv[1] which is usually the first command line parameter, was actually link.exe. It basically bumped all the arguments down by one offset. This in turn caused link.exe to try and interpret itself as its first input file!
Absolutely, unbelievably frustrating and hard to detect problem...
I read that it produces this error when you've included a header file (.h) in your Linker. I had this problem and solved it by removing the .h in Linker > Input > Addtl Dependencies