error LNK2005: void __cdecl mongo::uasserted(...) already defined - c++

I get the error
mongoclient.lib(assert_util.obj) : error LNK2005: void __cdecl mongo::uasserted(...) already defined in someOwnFile.obj
at linking, along with mongo::msgasserted and mongo::msgasserted.
In someOwnFile.cpp, I have
#include <string>
#ifdef WIN32
// Needed for mongo BSON.
#pragma warning(push)
#pragma warning(disable: 4800)
#pragma warning(disable: 4244)
#pragma warning(disable: 4267)
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#include <mongo/bson/bson.h>
#ifdef WIN32
#pragma warning(pop)
#endif
// ...
Why do I get the error? How can I fix it?

It seems that it has problems when including just this file.
Now I'm doing this:
#ifdef WIN32
// Mongo uses boost.filesystem. Include it here to force to link it in (via its pragma lib).
#include <boost/filesystem.hpp>
// Needed for mongo BSON.
#pragma warning(push)
#pragma warning(disable: 4800)
#pragma warning(disable: 4244)
#pragma warning(disable: 4267)
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#include <mongo/client/dbclient.h>
#include <mongo/client/dbclient_rs.h>
#include <mongo/client/gridfs.h>
#ifdef WIN32
#pragma warning(pop)
#endif
And this works.

Related

Type Redefinition Error (using Header Guards)

Disclaimer: C++ Redefinition Header Files (winsock2.h) doesnt solved my problem
In this project, im trying to take a screenshot and then compressing it with libjpeg-turbo. The problem is that i get errors like
"sockaddr": "struct" Type redefinition
and
"nothl": Redefinition
ScreenWorker.h:
#pragma once
#ifndef SCREENWORKER_H
#define SCREENWORKER_H
#include <string>
#include <vector>
#include <thread>
#include <turbojpeg.h>
#include <Windows.h>
#include "..\API\NetClient.h"
class ScreenWorker {
private:
NetClient* client;
public:
int delay = 30;
ScreenWorker(NetClient* client);
HBITMAP GetScreenBmp(HDC hdc);
void Update();
};
#endif
ScreenWorker.cpp:
#include "ScreenWorker.h"
ScreenWorker::ScreenWorker(NetClient* client) {
this->client = client;
Update();
}
HBITMAP ScreenWorker::GetScreenBmp(HDC hdc) {...}
void ScreenWorker::Update() {...}
The main.cpp (DLL-Entry):
#pragma once
#include "..\..\Base\API\API\GladOSClient.h"
#include "ScreenWorker.h"
using namespace std;
BOOL WINAPI DllMain(HINSTANCE Instance, DWORD Reason, LPVOID Reserved) {
return true;
}
NetClient.h (only Header part):
#pragma once
#ifndef NETCLIENT_H
#define NETCLIENT_H
#define _WINSOCKAPI_
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <vector>
#include <list>
#include <mutex>
#include <map>
#include <string>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#include "Utils.h"
#include "PacketHandler.h"
#include "Packet.h"
...
#endif
As you can see, i'm using header-guards everywhere, but nevertheless i get those errors. Seems like there is a problem with the including of "Windows.h"?
Thanks in advance!
Edit
I guess the problem has something to do with the way libjpegturbo handles the including of "Windows.h". Currently i have no real way of solving this issue. Maybe im trying exporting the function i need in a seperated DLL... hopefully this will solve it.
Classic issue.
Just remove this line from NetClient.h
#include <Windows.h>
The inclusion of <Winsock2.h> will pull in <windows.h> for you and correct all those redefine issues.

‘_snprintf’ was not declared in this scope

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

Using #pragma warning(push) and #pragma warning(pop) doesn't work as intended int his case

I'm compiling at a high warning level and while I have no warnings in my code, I get tens of thousands from included headers.So to disable them I do this:
#pragma warning(push)
#pragma warning(disable : 4365)
#include <D3Dcommon.h>
#include <d3d11.h>
#include <d3d11_1.h>
#pragma comment(lib, "d3d11.lib")
#include <d3dCompiler.h>
#include <D3D11Shader.h>
#pragma comment(lib, "D3DCompiler.lib")
#include <dxgiformat.h>
#include <dxgi.h>
#pragma warning(pop)
The problem is, I also have 5-6 disabled warnings in a header that is included everywhere in my code(for the entire project to see) and using the above pragmas for some reason enables them all back, giving me 150 thousand unwanted warnings all over the entire project.
The header with the 5-6 disabled warnings just lists them like this:(#### being different numbers)
#pragma warning(disable : ####)
#pragma warning(disable : ####)
#pragma warning(disable : ####)
#pragma warning(disable : ####)
......
I'm not using push or pop there, since I want them to be disabled everywhere.

Error C2146: syntax error : missing ';' before identifier

The line in question is:
extern BOOL logged_in;
Here are my includes:
#define _WIN32_WINNT 0x0403 // Very important for critical sections.
#define WIN32_LEAN_AND_MEAN // Good to use.
#pragma optimize("gsy", on) // Global optimization, Short sequences, Frame pointers.
#pragma comment(linker, "/RELEASE") // Release code
#pragma comment(linker, "/opt:nowin98")
#pragma comment(linker, "/ALIGN:4096") // This will save you some size on the executable.
#pragma comment(linker, "/IGNORE:4108 ") // This is only here for when you use /ALIGN:4096.
#pragma comment(linker, "/ALIGN:4096") // This will save you some size on the executable.
//default headers
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <time.h>
#include <stdlib.h>
#include <Winsvc.h>
#include <winuser.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <commctrl.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "shell32.lib")
Any ideas?
I don't see the line in the above code, but "missing ‘;’ before identifier" means that you are missing a semicolon before that line.

Why do I get warning C4081 on this #pragma?

I am in the habit of removing all warning reported in my code. I just like a clean build if possible. I used
#pragma comment(lib,"some.lib");
I get this warning:
warning c4081: expected 'newline'; found ';'
I am uncertain why that would create a warning. Could I get help on removing it?
Its the semi-colon at the end of the line. Its not needed for #pragma.
edit: The warning says it all: Expected a newline at the end of the pragma, but found a semi-colon instead.
Tested with VS2008
You can selectively, and temporarily disable all warnings like this:
#pragma warning(push, 0)
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/program_options.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/bind.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#pragma warning(pop)
Instead of 0 you can optionally pass in something like:
#pragma warning( push )
#pragma warning( disable : 4081)
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
// Some code
#pragma warning( pop )
#pragma warning(disable: 4081)
will disable the warning. but I can't repro the warning unless the syntax of the comment is wrong.
#pragma comment(lib "some.lib")
gives me warning 4081 but
#pragma comment(lib, "some.lib")
does not.
What is the text of your warning message?
Edit: I see now, forget adding a #pragma warning, just remove the ; from the end of the comment line. it's a syntax error.
You have not mentioned the compiler type and version, but I think you need to put the name without the ".lib" in second parameter (see here)