‘_snprintf’ was not declared in this scope - c++

I am working on a ffmepg c++ project which links a hpp file, in the hpp file:
#define snprintf _snprintf
#include <stdio.h>
#include <cstdio>
#include <unistd.h>
include <assert.h>
#if defined _MSC_VER && _MSC_VER >= 1200
#pragma warning( disable: 4244 4510 4512 4610 4146 4996 4005)
#define sprintf sprintf_s
#define _sprintf _sprintf_s
#define _snprintf _snprintf_s
#endif
snprintf (oc->filename, sizeof(oc->filename), "%s", filename);
it gives the error:
‘_snprintf’ was not declared in this scope
It is quite weird the error shows ‘_snprintf’ while what I use is 'snprint'. This code is wrote by others, I did not understand these #define he used. If i remove the line #define sprintf sprintf_s, it gives error:
segmentation fault(core dumped)
Due to ffmpeg is incompatible with C++, I have include the stdio.h and cstdio both within extern C and out of extern C, but the error continue show out. What is the problem? How to fix it?

At first change this line
include <assert.h>
with:
#include <assert.h>
and write this line
#define snprintf _snprintf
after includes and try compiling code

Related

Swig , C++ "include nested too deeply"

I am a beginner at c++ and I'm having this problem with nested include files. The code is too big to put here, but this is the part that I'm getting the error:
cvblob.h
#ifdef SWIG
%module cvblob
%{
#include "cvblob.h"
%}
#endif
#ifndef CVBLOB_H
#define CVBLOB_H
#include <iostream>
#include <map>
#include <list>
#include <vector>
#include <limits>
#include <opencv2/opencv.hpp>
#ifndef __CV_BEGIN__
#define __CV_BEGIN__ __BEGIN__
#endif
#ifndef __CV_END__
#define __CV_END__ __END__
#endif
#ifdef __cplusplus
extern "C" {
#endif
...
At line 4 in the above code (#iclude "cvblob.h"), the error happens:
[package_tracking/cvblob/cvblob.h:26]: (error) #include nested too
deeply
The guards are already used, but the error doesn't go away. Sorry that I am not able to put the entire code. If it is not possible to figure it out without the entire code, please answer these questions:
Should I put the guards (#ifndef CVBLOB_H) before the first line?
Is it necessary to put the guards in all the header files?
Thank you! I appreciate any suggestions.

LNK2005 & LNK1169 with extern function

I try to have a simple function which returns the current directory as a string:
Utils.h contains :
#ifndef __UTILS__
#define __UTILS__
#pragma once
#include <string>
#include <stdio.h> /* defines FILENAME_MAX */
#ifdef __linux__
#include <unistd.h>
#define GetCurrentDir getcwd
#else
#include <direct.h>
#define GetCurrentDir _getcwd
#endif
namespace
{
extern std::string _getCurrentDirectory()
{
char cCurrentPath[FILENAME_MAX];
if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
return "";
cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
return std::string(cCurrentPath);
}
}
#endif // __UTILS__
Despite extern declaration (redundant ?) and the creation of this specified Utils.h file, I still have linking problem:
Error 2 error LNK2005: "char * __cdecl getCurrentDirectory(void)" (?getCurrentDirectory##YAPADXZ) already defined in MyLibrary.lib(myobj.obj)
Error 3 error LNK1169: one or more multiply defined symbols found
This problem appears twice, and MyLibrary.lib doesn't even use or includes Utils.h
I really don't understand despite of the time I spent on it.
Does anyone have an idea ?
Thanks

Ffmpeg headers do not compile in windows

I am trying to link to ffmpeg under windows, but have run into difficulties. Inclusion of ffmpeg headers causes hundreds of compilation errors which i can't easily fix.
1) timestamp.h uses snprintf instead of _snprintf. I have tried to add it as a macro, like this:
#ifdef Q_OS_WIN
#define snprintf _snprintf
#endif
#define __STDC_CONSTANT_MACROS
namespace ffmpeg {
extern "C" {
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
} }
but it didn't help. Why would the macro not propagate inside?
2) There again, PRId64 isn't defined. I have defined __STDC_CONSTANT_MACROS before inclusion of timestamp.h, but definition isn't retrieved from inttypes.h
In compiler output, it looks like:
ffmpeg\include\libavutil/timestamp.h(42) : error C3861: 'snprintf': identifier not found
ffmpeg\include\libavutil/timestamp.h(42) : error C2146: syntax error : missing ')' before
identifier 'PRId64'
You did include them into a extern "C" right?
extern "C" {
#include <libavutil/imgutils.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
Is this a typo? You talk about "snprintf" ("n", not "m").
#define smprintf _snprintf
Even then, timestamp.h probably redifines it or includes something that does.

C2059 syntax error 'string' ?

extern "C"
{
#endif
#include <stdint.h>
#include <limits.h>
#include "attributes.h"
}
#endif
I added extern "C" { }
Then i got the C2059 string error
So i tried to use #endif, now i have another 4 errors.
Error 1 error C2059: syntax error : 'string' d:\c-sharp\c++
compiling\consoleapplication7\consoleapplication7\libavutil\rational.h 31 1
ConsoleApplication7
How can i fix this string error ?
At a guess, are you including this code from a C source file?
extern "C" { guards are only required (or understood) by C++. You can omit them from a C file, should include them in a C++ file and should guard them with a __cplusplus ifdef in a header file.
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <limits.h>
#include "attributes.h"
#ifdef __cplusplus
}
#endif

Compiling C code, does not see a #define'd constant

I am trying to compile a 8hz mp3 encoder - C code in QT Creator.
In a file l3psy.c that starts like this
#include <stdio.h>
#include "types.h"
#include "error.h"
#include "layer3.h"
#include "l3psy.h"
#include "fft.h"
#include "tables.h"
The build step complains about PI being undeclared here
for(i=0;i<BLKSIZE;i++) window[i] = 0.5*(1-cos(2.0*PI*(i-0.5)/BLKSIZE));
But types.h, which is obviously included, starts like this:
#ifndef TYPES_H
#define TYPES_H
#include <stdio.h>
#include <time.h>
#include "portableio.h"
#ifdef PI
#undef PI
#define PI 3.14159265358979
#endif
#define PI4 .78539816339745
#define PI64 .049087385212
therefore, there is no way for PI to be undeclared.
What can be the problem here?
also, aside from that stopper, I also get complains about "implicit declaration of function abort" and "implicit declaration of function exit" and "incompatible implicit declaration of built-in function 'exit'", but, they are standard functions of c, why would it complain?
For the first problem, about PI, see Pascal Cuoq's comment (that's all).
For the problems with implicit declarations being reported, you haven't included the relevant header(s) for those functions. IIRC exit and abort are declared by <stdlib.h. But check it out.
Cheers & hth.,