How to create C (not C++) console application in Visual Studio - c

I was trying with VS2012 to do following:
File->New Project->Win 32 Console application.
Go to properties C/C++->advanced and set Compile as C Code (/TC)
Rename source files extension cpp->c (not sure it is needed)
But project is compiled like C++ anyway.
What else should be done to make project compile as C?

From the MSDN,
By default, the Visual C++ compiler treats all files that end in .c as
C source code, and all files that end in .cpp as C++ source code. To
force the compiler to treat all files as C regardless of file name
extension, use the /Tc compiler option.
Also, Compiling with ANSI C in Visual Studio

It is compiling as C.
Not as C99 or C11 or C-plus-support-for-APIs-from-other-OSes.
Just plain ISO-standard-compliant C89/C90. Which is what the C compiler shipped with VS2012 supports.

Related

Compile Fortran program with IFORT and using object files (i.e. .lib) compiled in Microsoft Visual Studio

I have a basic question that I cannot find the answer to even after searching the web many times.
Is it possible to compile a Fortran program by IFORT that uses (as dependencies) object files (i.e. .lib) that were compiled by Microsoft Visual Studio C?
Yes, it is possible, and is even rather easy. You have several options for how to do this:
Add the MSVC library project to your Fortran Visual Studio solution and then use Project > Dependencies to add the C project as a dependent of your Fortran project *does not work if the C project creates a DLL)
Add the .lib from the C project to the Fortran project as if it were a source file
Name the C .lib in the Linker > Input > Additional Dependencies project property.
I generally recommend the first choice, as it means you don't have to fuss with different settings for Debug and Release projects. You do need to make sure that the C library is built to specify the same run-time library type (Debug vs. Nondebug, DLL vs. Static) as the Fortran project. This is in the Code Generation property page for C.
There is a worked example "Fortran Calls C" in the Intel Parallel Studio XE for Windows Sample Bundle.
You'll also need to understand how to call a C procedure from Fortran and ensure that the C arguments have compatible Fortran types. It works best if you make use of the Fortran standard's "C interoperability" features.

How to save an included library in C win32 Console application EXE file

I started programming in C and I have Visual Studio 2012.
I Write my program in Win32 ConsoleApplication and when I look in the Debug in my project there is an EXE file, and it work, but when i try to take it and run it from other computer that not runs Visual Studio it not working.
what i can do for it to work?
Thanks, Tomas.
go to the project properties and then: C/C++ -> Code Generation -> Runtime Library and then select the MT or MTD option depending in which configuration you are (for release MT and debug MTD)
The standard C functions like printf, fopen and strlen are delivered by the compiler vendor in a precompiled library and DLL. The default setting in projects is to not link all the standard functions directly into your program but keep them in an extra DLL and let your compiled program just use that DLL. On your machine with Visual Studio that DLL is present. On your other machine that DLL is not present.
There are two ways to solve that:
Look at the C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\redist and copy the DLLs you need to the target machine.
You can go the project settings "C/C++"/"Code Generation" and change the setting "Runtime Library" to a value without "DLL" in its name (/MT and /MTD). Then the compiler directly links all the standard C functions into your program and you can just copy your program to the other machine.
The reason Microsoft prefers the DLL is that with the DLL you can have multiple small programs in a folder each using the same DLL. If linking statically you end up with multiple bigger programs where each program has built in the same functions.

C DLL In Code::Blocks

I can't find anything about how to make a C DLL in Code::Blocks. Whenever I try and look it up it shows links to using C++ DLLs in managed programming languages. And Code::Blocks doesn't give an option for a C DLL. What do I do?
File->New->Project to show this dialog:
Then select Dynamic Link Library and away you go.
In Code::Blocks you create a new project, then select Dynamic Link Library (note: you can also, more easily create a Static Link Library, which is also available) and follow the prompts (as stated above).
In order to change your files from C++ to C, make sure the extension is .c not .cpp. Then you can right click on the included .c files (in the left column), select properties then under the Advanced tab you can change the compiler variable from "CPP" to "CC" and it will compile it using the C compiler. You will need to do this with each file in your project if you initially loaded them as CPP files. Edit: The COMPILER doesn't care about the filename extension, but the IDE you are using (Code::Blocks) DOES. The IDE will choose which compiler to use (GCC for C, and G++ for C++) based on the filename extension. It will choose the C++ compiler if your filename ends in .cpp, where as it will choose the C compiler by default if the extension ends in .c.
The Static Library option (further down the list on the selection screen) is an easier option if having a DLL is not that important to you. You simply load in your files, compile them and you're done. You don't have to redo your functions in any way or have a special header for it. It will create a library for you with the .a extension you can then link to your projects. The beauty is you won't need to provide a DLL file separately.

Linking different C source file in Visual Studio 2010

I am using visual Studio 2010 for compiling a bunch of source File. I am new to it and I wonder how the object files are linked during compilation. Like in Linux we have make file and rules. Does this suppport makefile? If not how can I compile and set flag during compilation.
You can use makefiles and other command-line tools to build using Visual C++ but more typically you handle this by using the GUI to set properties at the Project and File level - start here for info on this.
The Project/File properties adjust the command-line options passed to the compiler and linker for each compilation unit. You can view the actual command line that's used in the GUI too.

Writing C code in Visual C++ on VS2010

I appreciate the differences are negligible, but im doing some number crunching and so i want to use C. Ive just created a project in VS2010, chosen a C++ project and written some C. All executes fine, but
is this being created and executed in the fast(er) C compiler or the C++ because its a C++ project?
How can i specify that the code i wish to write is actually C and to be compiled and run as C?
The Visual Studio C++ compiler will treat all .c files as C language files and compile them as such.
Additional reference:
By default, the Visual C++ compiler
treats all files that end in .c as C
source code, and all files that end in
.cpp as C++ source code. To force the
compiler to treat all files as C
regardless of file name extension, use
the /Tc compiler option.
http://msdn.microsoft.com/en-us/library/bb384838.aspx
You are just being silly now. C is not guaranteed to be faster than C++ in any way - it's all compiled to native machine instructions in the end. If you want a true performance leap you should use another compiler, Intels for example, or use the GPU or something like that.
What will actually give you more speed is to use Intel's compiler, which is available as a plugin. The real-world differences are significant, especially for number crunching. The difference between C and C++ is dubious.
Here's a good place to start: link text
Since you're number crunching, you should consider using SIMD extensions, if possible. Using SIMD on Intel's compiler, vs. straight MS C compiled code, will give you some serious gain.

Resources