#include <math.h> in Visual Studio 2012 - c

This is a program I wrote :
#include "stdafx.h"
#include "math.h"
int main ()
{
int phi;
float c;
scanf_s("%d",&phi);
c=(pow(sin(phi)*cos(phi),-2))-(pow(tan(phi),2)+pow(tan(phi),-2));
if (c==2)
{
printf("C is 2,Don't Worry \n");
}
else
{
printf("Be Careful,C is not 2,How is this possible ?! \n");
}
}
The problem is Visual Studio won't recognize sin,cos,tan and pow.
what am I doing wrong ?
(as you see, the language is C!)

Despite that you haven't pointed out error message, most probable answer is that you need to explicitely tell your compiler, that you want to treat your code as C source file instead of C++ one. Visual Studio does not have project template for C console application "as is", but you may add source file manually. Just make sure that it has .c extension instead of .cpp.

I'm assuming you are actually compiling this as a C++ file as it should compile fine as a C file. You didn't specify the exact error message but in VS 2010 I get the error:
error C2668: 'sin' : ambiguous call to overloaded function
This is because your argument phi is an int and the compiler doesn't know which version of sin() to use. You can fix this by casting the integer to the desired type like:
int phi;
c = sin((float) phi);
or, simply change the type of phi to a float or a double.

Related

What does "declaration is incompatible" error mean in VS Code with C_lang linting?

I'm writing a simple code in C language, and this works.
Which compiles and excutes with no errors, gives the expected output.
#include <stdio.h>
int main(void) {
struct SiteTemplate {
int views;
};
int visit(struct SiteTemplate *site) {
site -> views++;
return 0;
}
struct SiteTemplate site;
site.views = 0;
visit(&site);
printf("%d\n", site.views);
return 0;
}
But in my VS Code, with C_Cpp linting is on, my IDE shows the following error and other problems with it.
declaration is incompatible with previous "visit" (declared at line 8)
Having a screenshot of it:
This error linting is really confusing me since my code works with gcc, it doesn't show any error when compiling.
And also, if I move my struct and function definition to the global level instead of inside main(), then the errors don't exist anymore... But what's the error declaration is incompatible? Or is there any problem with my code?
Click here to view the another screenshot to save whitespaces of this page.
By the way, the version of my VS Code is 1.52.0, with default C_Cpp linting.
Nested function definition is not standard C, it's supported by compiler extensions. According to C standard, any function definition needs to appear outside of any other function definition.

Matlab error compiling C files with mex

i'm trying to compile a program for image deblurring.
I try to run
mex apply_blur_kernel_mex.c
where the file apply_blur_kernel_mex.c have the following code
#include <mex.h>
#include <stdlib.h>
#include <math.h>
#include <matrix.h>
#include "ow_homography.h"
...
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
...
compute_homography_matrix(Ksharp, &theta_list[k*3], invKblurry, H);
...
}
The problem is in the function compute_homography_matrix that is in another file ow_homography.h
#ifndef OW_HOMOGRAPHY_H
#define OW_HOMOGRAPHY_H
#include "ow_mat3.h"
INLINE void compute_homography_matrix(const double *Ksharp, const double *theta, const double *invKblurry, double *H) {
double R[9];
/* Compute homography */
cp3(invKblurry,H);
rot3(theta[0],theta[1],theta[2],R);
mmip3(R,H);
mmip3(Ksharp,H);
}
This last operations (cp3, rot3...) are in another file ow_mat3.h that contains all the operations for the program.
So when i try to call
mex apply_blur_kernel_mex.c
i have the following problem:
Error using mex
Undefined symbols for architecture x86_64:
"_compute_homography_matrix", referenced from:
mexFunction in apply_blur_kernel_mex.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any suggestions to solve this problem?
Thank you all.
http://clang.llvm.org/compatibility.html#inline
C compatibility C99 inline functions
By default, Clang builds C code in GNU C11 mode, so it uses standard
C99 semantics for the inline keyword. These semantics are different
from those in GNU C89 mode, which is the default mode in versions of
GCC prior to 5.0. For example, consider the following code:
inline int add(int i, int j) { return i + j; }
int main() { int i = add(4, 5); return i; }
In C99, inline means that a function's definition is provided only for
inlining, and that there is another definition (without inline)
somewhere else in the program. That means that this program is
incomplete, because if add isn't inlined (for example, when compiling
without optimization), then main will have an unresolved reference to
that other definition. Therefore we'll get a (correct) link-time error
like this:
Undefined symbols: "_add", referenced from:
_main in cc-y1jXIr.o
By contrast, GNU C89 mode (used by default in older versions of GCC)
is the C89 standard plus a lot of extensions. C89 doesn't have an
inline keyword, but GCC recognizes it as an extension and just treats
it as a hint to the optimizer.
There are several ways to fix this problem:
1) Change add to a static inline function. This is usually the right solution if only one translation unit needs to use the function.
static inline functions are always resolved within the translation
unit, so you won't have to add a non-inline definition of the function
elsewhere in your program.
2) Remove the inline keyword from this definition of add. The inline keyword is not required for a function to be inlined, nor does it
guarantee that it will be. Some compilers ignore it completely. Clang
treats it as a mild suggestion from the programmer.
3) Provide an external (non-inline) definition of add somewhere else in your program. The two definitions must be equivalent!
4)Compile in the GNU C89 dialect by adding -std=gnu89 to the set of Clang options. This option is only recommended if the program source
cannot be changed or if the program also relies on additional
C89-specific behavior that cannot be changed.
All of this only applies to C code; the meaning of inline in C++ is
very different from its meaning in either GNU89 or C99.

Error Undefined reference to 'WinMain#16'

Can a program be written without main() function?
I have written this code and saved a filename as withoutmain.c
and getting an error as
undefined reference to 'WinMain#16'"
My code
#include<stdio.h>
#include<windows.h>
extern void _exit(register int code);
_start(){
int retval;
retval=myFunc();
_exit(retval);
}
int myFunc(void){
printf("Hiii Pratishtha");
return 0;
}
Please provide me the solution of this problem and also the proper memory construction of code and what is happening at the compiler end of this program.
Thank you!
Can a program be written without main() function?
Yes there can be a C program without a main function.
I would suggest two solutions.......
1) Using a macro that defines main
#include<stdio.h>
#include<windows.h>
#define _start main
extern void _exit(register int code);
int myFunc(void){
printf("Hiii Pratishtha");
return 0;
}
int _start(){
int retval;
retval=myFunc();
_exit(retval);
}
2) Using Entry Point (Assuming you are using visual studio)
To set this linker option in the Visual Studio development environment
/ENTRY:function
A function that specifies a user-defined starting address for an .exe file or DLL.
Open the project's Property Pages dialog box. For details, see
Setting Visual C++ Project Properties.
LClick the Linker folder.
Click the Advanced property page.
Modify the Entry Point property.
OR
if you are using gcc then
-Wl,-e_start
the -Wl,... thing passes arguments to the linker, and the linker takes a -e argument to set the entry function

cacosf (Complex arc cos) function in C returns indefinite

I have an algorithm coded in MATLAB, which contains complex arc cos of some value (computation requires arccos of 15, which is approximately 3.4i). I want to code C or C++ counterpart of this code running on my Windows 7 PC. Actually, I want to produce it as a mex function compiled with Visual Studio C++.
I included "complex.h" and used cacosf function (complex arccos returning float _Complex) but I could not compile it as a mex function because Visual C++ compiler does not have "complex.h" support. However, mex file can take libraries as input, so I can compile my c code with another compiler that MATLAB does support (for example mingw, I integrated it to matlab with gnumex utility.) I downloaded Bloodshed C++ IDE which uses mingw at backend, I can compile my c++ code. The following C++ code represents a similar operation to my goal:
#include <stdio.h>
#include <complex.h>
int main() {
float _Complex myComplex;
myComplex = cacosf(5);
printf("Complex number result of acos(5) is : %f + %fi \r\n",crealf(myComplex),cimagf(myComplex));
return 0;
}
The output should be:
Complex number result of acos(5) is : 0.000000 + -2.292432i
However I get
Complex number result of acos(5) is : -1.#IND00, -0.000000
When I compile my C++ code with Linux GCC on Ubuntu 14.04 computer with Eclipse CDT Luna I get
The output should be:
Complex number result of acos(5) is : 0.000000 + -2.292432i
Where can I be wrong? Why can't I compile this code in Windows + mingw setup?
Note: I can compute cacosf(0) as 1.570796 + -0.000000 when I use mingw.
What version of mingwrt are you using? With mingwrt-3.21.1, the following works for me, (cross-compiling on a Linux host, and running under wine):
$ cat foo.c
#include <stdio.h>
#include <complex.h>
int main()
{
double _Complex Z = cacos(5.0);
printf( "arcos(5) = (%g, %gi)\n", __real__ Z, __imag__ Z );
return 0;
}
$ mingw32-gcc -o foo.exe foo.c
$ ./foo.exe
arcos(5) = (0, -2.29243i)
This seems to be consistent with your expected result. However, if you use any mingwrt version pre-dating mingwrt-3.21, (and the less said about utterly broken mingwrt-4.x the better), then there is a known bug resulting from arbitrarily deeming any purely real cacos() argument value greater than (1.0, 0.0i) to be outside the valid domain, (as would be the case for acos() on its real part), which would yield the result you report.
Visual C++, as the name says, is a C++ compiler. C++ uses the <complex> header and std::complex<float> type. Since C++ has overloading, you can call std::acos for complex values too.
Your code is in fact C, which is no longer supported by MSVC++. (They stopped doing that back in 1996 or so)

C Build error when getting the value of sin()

I have recently started learning C as a side project. I am working under OpenSuse with the latest NetBeans using the GCC as toolset for compiling.
One of the very first programs that I made was this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*
*/
int main(int argc, char** argv) {
double rad = 1;
double result = 0;
result = sin(rad);
return (EXIT_SUCCESS);
}
This is a simple, no-brainer example that should have worked without a problem. However, I get a Build Error: Exit code 2(error in line 18, undefined reference to sin) when trying to compile.
Interestingly enough, if I remove the assignment of the value of sin(rad) to result OR replace rad with a hard coded value, the program compiles just fine.
What am I doing wrong here?
In C, you need to link to the math library:
Add this to the command line options:
-lm
Be sure that your are linking with the math library.
$ gcc myprog.c -lm

Resources