moved from cygwin to VisualStudio2013, error LNK2019, snprintf(), c - c

I'm trying to run a unix compiler-project written in c with MS Visual-Studio 2013 and I can't get rid of the following error:
error LNK2019: unresolved external symbol "_snprintf" referenced in
function "PUBLIC void SyntaxError( int Expected, TOKEN CurrentToken )"
If I get it right it is a problem where VisualStudio can't find the body/declaration from the snprintf() function, which should be defined in stdio.h.
The project works fine with cygwin. I had to add _CRT_SECURE_NO_WARNINGS to preprocessor settings to get this far, but i don't think that has a influence.
Here is the named function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "line.h"
#include "strtab.h"
#include "scanner.h"
[..code..]
PUBLIC void SyntaxError( int Expected, TOKEN CurrentToken )
{
char s[M_LINE_WIDTH+2];
snprintf( s, sizeof(s), "Syntax: Expected %s, got %s\n", Tokens[Expected], Tokens[CurrentToken.code] );
Error( s, CurrentToken.pos );
}
If you can help me or there is anything else you need to know please tell me. It's my 3rd day now and I am running out of ideas ;).
So far... Tobias

The name of this function with the MSVC compiler is _snprintf() with an underscore.

Related

Why is #include <stdio.h> is underlined in red?

I have this basic code in C. A program to say 'Hello World'. And the first line That say #include <stdio.h> is underlined in red as though an error is occurred.
#include<stdio.h>
main()
{
printf("hello, world\n")
}
I'm using vs code to run the program, is it a bug in vs code? or maybe a problem in the installation of C?
please help.
I don't understand where the problem originates so no actions have been pursued.
Add space between #include and <stdio.h>
Also, specify the return type of your main function.
Also at the end of printf("hello, world\n"), there should be semi colon
#include <stdio.h>
void main(){
printf("hello, world\n");
}
it is void since it is not returning anything.

#include <windows.h> causes errors

I'm trying to use GetTickCount() from the windows.h header file. Right now my code looks like this:
#include <stdio.h>
#include <string.h>
#include "declarations.h"
#define INPUTBUFFER 400 * 6
#define START_POS "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
#include <windows.h>
void interface() {
.....
.....
}
The compiler returns 2 errors:
Expected '{' before '(' token (pointing to the '(' after "interface"
2 or more data types in declaration specifiers (pointing to "interface")
When I comment out #include < windows.h >, the interface function works just fine. I don't even have GetTickCount() in my code yet. I checked the gcc path to make sure windows.h is there, so I'm not sure why I'm getting this error.
As RbMm points out in a comment, the windows headers contain "#define interface struct", so the compiler sees your code as "void struct() { ....}" which is not valid C++
You need to either rename your function (probablly the better option) or add a "#undef interface" before your function.

C error: conflicting types for function and previous declaration was here (not duplicate)

Apologies for the dumb question. I checked all similar questions for the same error on stackoverflow, but it didn't help me understand why this error is happening in the following code.
I have one additional header file and a source file, which is included in the main file, and when I compile, I am getting the following error. I am trying to pass the char** argv from the main() to another function defined in another header file.
#include "include/Process.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
if (argc < 2) {
printf("Please provide a path to file\n");
return (EXIT_FAILURE);
}
Process(argv);
Process.h:
#pragma once
extern void Process(char** path);
Process.c:
#include <stdio.h>
#include "../include/Process.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>
void Process(char** path) {
printf("%s\n", path[1]);
}
It gets compiled but the warning is
./src/Process.c:22:6: error: conflicting types for ‘Process’
void Process(char** path) {
^
./include/Process.h:17:6: note: previous declaration of ‘Process’ was here
extern void Process(char** path);
^
However, the warning disappears when I change the type of path from char** to char* and pass argv[1] instead of argv.
I am clueless why this is happening like this, and according to
this similar post, I tried adding a forward declaration for char** path above extern void Process(char** path); in the Process.h file, but it didn't help either.
Why is this error thrown when using char** path?
Why it disappears when I use char* path?
So far, I am able to see the program running, even with this warning. Is it safe to ignore this warning? If not, what could be the possible effects it can have during runtime?
Using gcc version 4.9.2 (Ubuntu 4.9.2-10ubuntu13)
Thanks.
Try putting your custom includes after the system includes.
It might be possible that the custom include defines a macro which interferes with the system includes. To minimize the risk of this, I always put the Standard C includes first, then any OS includes, and then third party libraries, and then my own ones
In theory the custom include shouldn't do this, and the system includes should only use reserved names, but in practice this doesn't always happen.

Strange problem with compiling c code in xcode when including glext.h

I have this simple code
#include <stdio.h>
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
int main (int argc, const char * argv[])
{
printf("Hello, World!\n");
return 0;
}
If I comment out the line with "glext.h" it compiles and runs fine in xcode 4 if I uncomment that line I get 345 errors most of them 'expected * before *' ...
What is going on?! both gl.h and glext.h are inside the OpenGL framework but no matter if I incluhe it or not I get the same error. I tried GCC 4.2 as well as LLVM GCC 4.2 and LLVM (in this case 21 semantic and parse errors).
I am sure my lack of experience with C is causing this but I am surprised gl.h has no problem but glext.h has.
Even if I try to compile in from the command line by gcc I get lots of
/System/Library/Frameworks/OpenGL.framework/Headers/glext.h:3137: error: expected ‘)’ before ‘const’
Any ideas?
It's a bug with glext.h. If you look at that file, you'll see that it has a bunch of definitions that use GLenum, but GLenum isn't defined anywhere in that file. So, before you include glext.h, you need to include a file that defines GLenum. The easiest thing to do is to include gl.h first instead of second:
#include <stdio.h>
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
Switch these two lines around:
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
And it should work.

why I get error: 'strcmp': identifier not found (visual studio 2010)

why do i get error: 'strcmp': identifier not found in visual studio 2010 C++ Express
#include <string.h>
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d",(int)strcmp( "str1", "str2" ));
return 0;
}
Thanks
:( #include <string.h> :(
#include "stdafx.h"
Fun quirk of the MSVC compiler, it generates the exact same error when you compile it like that. Yes, not a lot of 'fun'. It skips everything to find the stdafx.h precompiled header include directive. The string.h doesn't actually get included. Fix:
#include "stdafx.h"
#include <string.h>
Always put the stdafx.h include first.

Resources