How to compile C source from Visual Studio? - c

i have simple C source file created and compiled nicely on GNU gcc/cc. However when I wanted to compile in Visual Studio on Windows it gives me lots of errors. Is there any particular settings to set for VS compiler in order to compile nicely C source code.thanks
C source (standard style ?)
#include <..>
//function prototype
int main(){
// my code here
}
/* all my other functions below this */
Just found out I can use cl.exe . would try this first.

Related

copying c code from replit to visual studio on mac

So I went to copy my c code from replit to visual studio because I have to work on an assignment online.
I have a floating.h file I created for functions like toDouble(unsigned int) etc. when I include the file in my main.c and call the code, output says “_toDouble” are undefined symbols for architecture x86_64.
My actual implementation of the functions are in my floating.c file.
My floating.h has for example
“double toDouble(struct floating f)”

how can i fix windows c_api error with tensorflow.dll?

I tried to compile on windows c program with tenserflow c api and tenserflow.dll from https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.12.0.zip founded on https://www.tensorflow.org/install/lang_c.
This example:
#include <stdio.h>
#include <tensorflow/c/c_api.h>
int main() {
printf("Hello from TensorFlow C library version %s\n", TF_Version());
return 0;
}
Compiling is success, but when i have run it, i recieved a mistake that libtenserflow.so not found. Its look like that tensorfow,dll from https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.12.0.zip was builded with some mistakes for windows sistem, becaurse libtensorflow.so is a target for Linux.
Can you explain or fix this?
I guess it looks for tensorflow.so because you were using GCC tools on VS Code's WSL mode (or other IDEs). But in order to load DLL you need to have Visual Studio.
Here's a simple process to run the Tensorflow for C demo:
Create a new project in Visual Studio;
Configure the project properties(assume the Tensorflow path is C:\tensorflow\; replace it with yours):
C/C++ > General > Additional Include Directories, add "C:\tensorflow\include\"
Debugging > Environment, add "PATH=C:\tensorflow\lib\;%PATH%"
Don't forget the "PATH=" before your tensorflow.dll path.
Compile and run.
You may also add the Tensorflow path to system environment (replace C:\tensorflow\ with your path):
SET PATH=%PATH%;C:\tensorflow\lib\
P.S. If you don't like the Visual Studio IDE and prefer to use Tensorflow with command line mode, try Bazel for Windows instead.

variable "using" is a not a type name?

I am currently trying to create an application in C as an assignment for school, and my professor requires me to use the following to make it compatible with his compiler:
#pragma warning(disable: 4996)
#include<string>
#include<stdlib.h>
#include<time.h>
(I am using visual studio)
And for some reason #include <string> throws up a bunch of run time errors mainly consisting of the one in the title of the question. It pops up within a bunch of other dependencies ranging from cmath, cstudio, cstdlib, cstring, etc. So the bottom line is, can anyone tell me how to fix it? Thanks!
In visual studio you can get this error if you extension is '.c' VS studio will assume the file is type c and not c++ and use the c compiler and not the c++.
In the project settings under advanced options you can select compile as C++ and then files will all be compiled as C++. If you scroll down you will find the flag '-x' on the command line as added to it.
If you are using the gcc compiler this is the same as adding the following to the compiler command '-x c++'
Check the gcc reference for this flag.
The #include <string> is a C++ header file and therefore incompatible with C.
As others have suggested, you can change this to #include <string.h> so that you have a valid C header, however if your professor dictated that you use that specific set of headers, then you need to change your source file to a .ccp file instead of a .c file.
You're including C++ header file. Use #include <string.h>
You are including a C++ library in C code. The header files without any extension are C++ header files. Having ".h" extension are C header files. C++ also accepts C header files still they have no extension. They have given same names having "c" prefix and no ".h" extension.
for example
<string>
is a C++ header
<string.h>
is a C header
<cstring>
is a C header but in a C++ code.

Fatal error when using FILE* in Windows from DLL

Recently, I found a problem with Visual C++ 2008 compiler, but using minor hack avoid it. Currently, I cannot use the same hack, but problem exists as in 2008 as in 2010 (Express).
So, I've prepared for you 2 simple C file: one for DLL, one for program:
DLL (file-dll.c):
#include <stdio.h>
__declspec(dllexport) void
print_to_stream (FILE *stream)
{
fprintf (stream, "OK!\n");
}
And for program, which links this DLL via file-dll.lib:
Program:
#include <stdio.h>
__declspec(dllimport) void print_to_stream (FILE *stream);
int
main (void)
{
print_to_stream (stdout);
return 0;
}
To compile and link DLL:
cl /LD file-dll.c
To compile and link program:
cl file-test.c file-dll.lib
When invoking file-test.exe, I got the fatal error (similar to segmentation fault in UNIX).
As I said early, I had that the same problem before: about transferring FILE* pointer to DLL. I thought, that it may be because of compiler mismatch, but now I'm using one compiler for everything and it's not the problem. ;-(
What can I do now?
UPD:
I've found solution:
cl /LD /MD file-dll.c
cl /MD file-test.c file-dll.lib
The key is to link to dynamic library, but (I did not know it) by default it links staticaly and (hencefore) error occurs (I see why).
P.S. Thanks for patience.
Potential Errors Passing CRT Objects Across DLL Boundaries
There is a specific example for your situation in here. Depending on how you compile your DLL and program, you might have separate copies of the CRT which will result in an access violation.

Visual Studio: Create a Hello World app in C?

How can I create a basic C app in Visual Studio, be it 2010 Ultimate or 2008 Professional? I have searched through the project templates, and can find plenty for C++, but none for C.
(I'm hoping that the compiler and debugger will be built in.)
New project/Win32 Console Application/Empty project.
Add a file called "hello.c" (important that it's .c)
Type out a basic hello-world:
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
Compile, execute... PROFIT!
Visual Studio doesn't have a separate compiler for C, it uses the C++ compiler for C code. You can tell it to restrict itself to legal C syntax by using a compiler switch or by renaming the .cpp file to .c
Edit: I'm still using 2005 on this machine, so it might not be in the same place, but try this
Right click on the main.cpp in the solution explorer pane (on the right).
Choose Properties (bottom of the menu)
Open up the C/C++ group of properties
choose Advanced page
Change "Compile As" property to "Compile as C Code (/TC)"
In addition to changing the file extension, you may need to change the compiler settings:
Settintgs of Application->C/C++->addition->compiler such...
You must take /TC for basic C and /TP for C++.
https://msdn.microsoft.com/ru-ru/library/032xwy55.aspx
Good Luck.

Resources