VERY simple C program won't compile with VC 64 - c

Here is a very simple C program:
#include <stdio.h>
int main (int argc, char *argv[]) {
printf("sizeof(short) = %d\n",(int)sizeof(short));
printf("sizeof(int) = %d\n",(int)sizeof(int));
printf("sizeof(long) = %d\n",(int)sizeof(long));
printf("sizeof(long long) = %d\n",(int)sizeof(long long));
printf("sizeof(float) = %d\n",(int)sizeof(float));
printf("sizeof(double) = %d\n",(int)sizeof(double));
return 0;
}
While it compiles fine on Win32 (command line: cl main.c), it does not using the Win64 compiler ("c:\Program Files(x86)\Microsoft Visual Studio 9.0\VC\bin\amd64\cl.exe" main.c). Specifically, it sais "error LNK2019: unresolved external symbol printf referenced in function main". As far as I understand this, it can not link to printf, right?
Obviously, I have Microsoft Visual C++ Compiler 2008 (Standard enu) x86 and x64 installed and am using the 64-bit flavor of Windows (7).
What is the problem here?
UPDATE:
OK, now I ran vcvarsall.bat amd64. It sais "The specified configuration type is missing. The tools for this configuration might not be installed.". As mentioned, the compiler clearly is installed??

If you look at vcvarsall.bat it will print the specified configuration missing statement if certain batch files are missing.
For the amd64 option this file must exist:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64\vcvarsamd64.bat

There's something wrong with your setup. It compiles & link fine for me.
D:>cl test.cpp Microsoft (R) C/C++
Optimizing Compiler Version
15.00.30729.01 for x64 Copyright (C) Microsoft Corporation. All rights
reserved.
test.cpp Microsoft (R) Incremental
Linker Version 9.00.30729.01 Copyright
(C) Microsoft Corporation. All rights
reserved.
/out:test.exe test.obj
D:>
Did you run vcvarsall.bat with the right options to set up the environment variables before you try running cl.exe? Something along the line of
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86_amd64

Are you on a 32bit or 64bit system? I'm guessing from the path that the 64bit standard libraries aren't available, at least not in that directory.

Related

Why can Visual Studio 2017 Developer Command Promt only open "stdio.h" in Administrator mode?

I am trying to do simulation using Delmia. This requires a functioning C compiler. When i test my C compiler in Delmia, which tries to compile a very simple source file that includes stdio.h, I get the error "Cannot open include file: 'stdio.h': No such file or directory":
Compiling and linking the model (Visual C++).
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.6.2
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
Guessed compiler version: 1913
"Testing 32-bit compilation"
dsmodel.c
dsmodel.c(1): fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory
Error generating Dymosim.
It seems to be entirely the same issue as:
Visual Studio 2017 for Dymola cannot open stdio.h
His solution is not descriptive enough for me to solve my issue, as his "IT dept" apparently solved it for him, but he mentions allowing access to regedit which I do not understand the reason for in this context.
I have tried to recreate the error that Delmia generates by compiling dsmodel.c with VS2017 Developer Command Promt. If I run the command prompt in Administrator it compiles fine, if I do not, I get the samme error as Delmia outputs. Why is this?
If I find a solution such that I can compile it with command prompt without being in Administrator mode, then should Delmia not also be able to? How can i achieve this?
Some additional information: I have installed Visual Studio not on my main drive, and the Windows SDK is in program files on my main drive. Does this have any relevance?
Edit: For anyone else with this issue in Delmia, I would suggest simply installing another compatible compiler as a work-around. I installed MinGW which works well, even though this additional compiler takes up 500 MB. Remember that it has to be 64-bit. I used the "MingW-W64-builds" from http://mingw-w64.org/doku.php/download

How to compile static .lib library for Windows in Linux or Macos

I am searching way to compile static library for Windows in Linux or Macos, there seems to be cross compiler to generate .a library for Windows like this one, but that is not what I want, what I want is a .lib static library file for Windows, preferably for Visual Studio. I know I can run a Windows virtual machine and using Visual Studio, but that is too heavy, and can't be done in command line.
For unix-like OSes (Linux, MacOS, etc) a static library means
an ar archive of object files. ar is the GNU general
purpose archiver. It doesn't care what kind of files you stick into an archive. It's
just the custom to call it "a static library" when they happen to be object files. And
it's also just a custom for an ar archive to be called *.a. You can call it
*.lib, or anything.
For Visual Studio, a static library means an archive of PE-format object files
usually created by the Microsoft tool LIB.
The format of an Microsoft LIB archive is in fact the same as that of a Unix ar archive. Microsoft
just adopted it, long long ago.
So if you compile some PE object files on Linux using your distro's PE cross-compiler
then archive them into a *.lib with ar, you've got yourself a static library that's good to go in Windows
with the Visual Studio compiler.
Well, you have as long as those object files have C binary interfaces.
If any of them have C++ interfaces, they're useless: the Microsoft and GCC C++ compilers use different name-mangling protocols and are otherwise ABI incompatible.
Demo
We start in linux with some source code for the static library:
hello.c
#include <stdio.h>
void hello(void)
{
puts("Hello world");
}
Cross-compile:
$ x86_64-w64-mingw32-gcc-win32 -o hello.obj -c hello.c
Make the static library:
$ ar rcs hello.lib hello.obj
Then a program that's going to be linked with hello.lib:
main.c
extern void hello(void);
int main(void)
{
hello();
return 0;
}
Now we hop into a Windows 10 VM where we're looking at the the files we've
just created through a shared folder:
E:\develop\so\xstatlib>dir
Volume in drive E is VBOX_imk
Volume Serial Number is 0000-0804
Directory of E:\develop\so\xstatlib
03/12/2017 18:37 72 main.c
03/12/2017 18:29 978 hello.lib
03/12/2017 18:26 66 hello.c
03/12/2017 18:27 832 hello.obj
4 File(s) 1,948 bytes
0 Dir(s) 153,282,871,296 bytes free
Compile and link our program:
E:\develop\so\xstatlib>cl /Fehello.exe main.c hello.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25547 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
main.c
Microsoft (R) Incremental Linker Version 14.11.25547.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:hello.exe
main.obj
hello.lib
Run it:
E:\develop\so\xstatlib>hello
Hello world

Compiling C code in cmd prompt

I use sublime text as my text editor and the cmd prompt to compile and run the code. The cmd prompt out of no where will no longer update the exe with the new code added to the program. Has anyone had this problem? It was working the other night and I have not changed any settings on the computer that would make this not work. Also the program has no errors
If you have installed Visual Studio 2015 (or other edition) on Windows 10 and create source code file with .c extension, you can use cl.exe.
It is better to use Visual C++ developer command prompt (can be started from Start button).
C:\Program Files (x86)\Microsoft Visual Studio 14.0>cl prog.c
Visual C++ developer command prompt provides environment settings like Path.
If you just run cl.exe without parameters you should see something like
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23918 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
This way is not for Mingw. But, perhaps, compiler is not limiting... so Visual Studio can work at the same PC. Just as an option

Custom C dll for MATLAB - loadlibrary error

I have a custom dll written for MATLAB and it works fine on our development machines. However, when I try it on a clean machine with no development tools, I get the message:
>> loadlibrary CMatLab CMatLab.h
??? Error using ==> loadlibrary at 279
Microsoft Visual C++ 2005 or 2008 is required to use this feature.
After some research it seemed clear that a compiler was required to parse the header file at runtime, so I installed the standard Windows SDK, ran mex -setup to select the compiler but I still get the same error message. This is what I did for select the compiler.
>> mex -setup
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? mex -setup
Select a compiler:
[1] Microsoft Visual C++ 2008 SP1 in C:\Program Files (x86)\Microsoft Visual Studio 9.0
[0] None
Compiler: 1
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 SP1
Location: C:\Program Files (x86)\Microsoft Visual Studio 9.0
Are these correct [y]/n? y
***************************************************************************
Warning: MEX-files generated using Microsoft Visual C++ 2008 require
that Microsoft Visual Studio 2008 run-time libraries be
available on the computer they are run on.
If you plan to redistribute your MEX-files to other MATLAB
users, be sure that they have the run-time libraries.
***************************************************************************
Trying to update options file: C:\Users\adriane\AppData\Roaming\MathWorks\MATLAB\R2010b\mexopts.bat
From template: D:\Matlab\bin\win64\mexopts\msvc90opts.bat
Done . . .
I really do not want to install Visual Studio on this machine as it reduces it's utility as a testbed for release builds of our other tools and software. Any ideas anyone? I see that others have the same problem but I did not see a clear solution. The OS is Windows 7 pro 64 bit. The dll was built with VS2008.
Try using the 'MFILENAME' option to loadlibrary to produce a "protofile", which can be used in the future to load the DLL via the #PROTOFILE syntax. Generate the file on your development machine, and bring it along to the test machine.
So, on the dev machine:
loadlibrary('CMatLab', 'CMatLab.h', 'mfilename', 'cmatlab_proto');
Bring along to the test machine the DLL, the file labelled 'thunk', and cmatlab_proto.m. On the test machine, run:
loadlibrary('CMatLab', #cmatlab_proto)

Compiling C-code from the Command Prompt in Windows?

I want to compile C code from the Command Prompt in Windows. I have added the environment variable to the PATH and I can compile .cs files with: csc app.cs
That's OK, but how do I compile app.c?
You do this:
cl app.c
Here's a complete transcript, including setting up the environment for Visual Studio 2005 (change "8" to "9.0" for Visual Studio 2008).
C:\src\tests>"\Program Files (x86)\Microsoft Visual Studio 8\vc\bin\vcvars32.bat"
Setting environment for using Microsoft Visual Studio 2005 x86 tools.
C:\src\tests>type app.c
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
return 0;
}
C:\src\tests>cl app.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
app.obj
C:\src\tests>app
Hello world!
MinGW provides a popular command-line GCC compiler for Windows.
open command prompt
type path c:\tc\bin
goto your project folder in command prompt
type tcc filename.c
after compilation type filename

Resources