Command line compiling of C++ in Windows in Python script

600 Views Asked by At

The idea is to compile a C++ program. It contains a main.cpp, a printer.cpp, a printer.h, a scanner.cpp and scanner.h. These source files have one function print "hello". Now I am trying to create the object files and executables from the command line through a Python script and using cl.exe.

The error I get is LNK2019 so I know the issue is in the linking. I have looked though the options and I am using this for my Python !!

build = subprocess.Popen(['vcvarsall.bat', 'amd64_x86', '&&', 'cl',   'kernel32.lib',...[skipping some files]'uuid.lib','/I' + qtpath,'C:\\Users\\ROY_S\\Desktop\\CppMaker\\main.cpp','/ZI','/Gm','/EHsc','/MDd','/GS','/Fo'+path,'/Fe'+path+'main.exe','/link','/LIBPATH:'+qtlib,'/DEFAULTLIB:'+ qtlib+'QtMainIsar','/DEFAULTLIB:'+ qtlib+'QtCore','/DEFAULTLIB:'+ qtlib+'QtGuiIsar4','/DEFAULTLIB:'+ qtlib+'QtNetwork','/DEFAULTLIB:'+ qtlib+'QtOpenGLIsar4','/DEFAULTLIB:'+ ...[skipping...] qtlib+'QtWebKitIsar4','/INCREMENTAL','/NOLOGO','/TLBID:1','/DYNAMICBASE','/MANIFEST','/NXCOMPAT','/ERRORREPORT:PROMPT','/MACHINE:'+'X86','/OUT:'+path+'main.exe' ]  , stdout=subprocess.PIPE)

I removed some libs so that its easier to read. I dont understand how do I link everything even after specifying the lib files in my script.

I can also move on to other solution instead of using cl.exe. Anything that has easy control over the commandline I am okay with it.

3

There are 3 best solutions below

0
On

In windows I think it's a good idea to use Visual Studio project/solution and build it using MsBuild. Project/solution file may be created manually or in Visual Studio. You may also create properties in project/solution and pass them in MsBuild (see MsBuild documentation)

0
On

Visual Studio is the easiest solution although if you REALLY don't like visual studio you can get gcc through https://www.cygwin.com/

0
On
check = subprocess.Popen(['vcvarsall.bat', 'amd64_x86', '&&',    'msbuild',Path,'/p:configuration=%s' % Configuration])

This solves my issue.Instead of playing with the cl.exe , I make a template of a vcxproj file. Then my code adds the sources and header files in the vcxproj file and I compile that using msbuild. I feel its a much easier solution because I dont have o care about the individual libraries to add and can just add a propery sheet to my vcxproj file .

Also see https://msdn.microsoft.com/en-us/library/ms164311.aspx