Ali Awan
Ali Awan

Reputation: 352

Makefile returns an error when I try to use cl.exe as the compiler

I have activated vcvarsall.bat in my makefile, however I still get this error when I try compiling my program:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.11.1
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
process_begin: CreateProcess(NULL, cl /Z7 /W3 C:\Dev\LearningC++\main.cpp
, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [build] Error 2

[Process exited 2]

Makefile:

all: build run

build: main.cpp
    @call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
    @cl /Z7 /W3 C:\Dev\LearningC++\main.cpp

run:
    @bin\main.exe

When I type the commands manually, the code does get compiled and I produce and executable which works, however I also get these weird warning messages from the compiler:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MS
VC\14.29.30133\include\ostream(746): warning C4530: C++ exception handler
 used, but unwind semantics are not enabled. Specify /EHsc
main.cpp(4): note: see reference to function template instantiation 'std:
:basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_t
raits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char
 *)' being compiled
Microsoft (R) Incremental Linker Version 14.29.30133.0
Copyright (C) Microsoft Corporation.  All rights reserved.

I am using Windows 10.

Upvotes: 0

Views: 577

Answers (1)

MadScientist
MadScientist

Reputation: 100856

In a makefile recipe, every command line is run in its own shell. In Windows the vcvarsall.bat file sets a bunch of environment variables and environment variables are in effect only for the current shell; when the shell exits they are gone. When you run:

build: main.cpp
    @call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
    @cl /Z7 /W3 C:\Dev\LearningC++\main.cpp

first make starts a shell and runs the call in it which sets the Visual Studio environment, then the shell exits and all those variable settings are lost. Then make starts another shell and runs cl but the environment settings it wants are no longer there so it fails.

I think the way to put multiple commands on a single line in Windows cmd.exe is using & so you can try rewriting your makefile like this:

build: main.cpp
    @call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 \
      & cl /Z7 /W3 C:\Dev\LearningC++\main.cpp

(note the backslash at the end of the call line, then the &).

Upvotes: 1

Related Questions