Compiling C code with VLD - c++

I'm trying to compile the SDL libraries with Visual C++ (2010), and with Visual Leak Detector as to find a memory leak in another program that calls SDL.
The problem is that vld.h is a C++ library, and SDL.c is a C program. Accordingly, when I #include , the source doesn't compile as VLD seems to use a C++ specific constructs:
typedef int (__cdecl * VLD_REPORT_HOOK)(int reportType, wchar_t *message, int *returnValue);
__declspec(dllimport) int VLDSetReportHook(int mode, VLD_REPORT_HOOK pfnNewHook);
I've attempted compiling SDL.c as a C++ program, but I get a plethora of errors.
Is there any way I can include VLD in SDL?

(with thanks to #Matias Valdenegro)
The problem was the wchar_t. To be able to recompile SDL (and I assume other C source files) to include VLD, add
#include <wchar.h>
to both vld.h and vld_def.h. SDL will then happily compile, and can be used with an SDL program to detect memory leaks stemming from Surfaces and the like.

Related

Contradictory unresolved external symbol + unused library altogether with VS2017 and FFMPEG 4

I'm having a small c++ project in Windows with FFmpeg 4.0.2. However, I have weird problem: I'm compiling in x64, having x64 libraries, and having correct link input, but I got the LNK2019 error AND at the same time "unused libraries" in the linker output /VERBOSE:
1>Unused libraries:
1> I:\lib\ffmpeg-4.0.2-win64\lib\\avcodec.lib
1> I:\lib\ffmpeg-4.0.2-win64\lib\\avutil.lib
I checked manually that lib files are x64. I:\lib\ffmpeg-4.0.2-win64\lib\ is in the LIBPATH.
Same symptoms with ICC.
How could this happen ?
To include the headers of ffmpeg in a C++ program you have to take in account that ffmpeg uses the C calling conventions. Otherwise your linker will expect C++ name mangling on the function names. However, since ffmpeg is straight C, you'll have to tell you compiler this.
For example if you include avformat.h in your program do it as follows.
#ifdef __cplusplus
extern "C" {
#endif
#include <avformat.h>
#include <avcodec.h>
#include <avutil.h>
#ifdef __cplusplus
}
#endif
The same goes for the other ffmpeg headers.

How to use a C struct in C++ code?

I am trying to write a program that should use a C library (the LIS library) in a C++ program. There seems to be a problem with the creation/initialization of struct objects.
When I run the example program on the wikipediapage: http://en.wikipedia.org/wiki/Lis_%28linear_algebra_library%29 it runs like a charm, but of course that is compiled as a C program.
In my C++ code I do it as follows:
#include "stdafx.h"
#include <iostream>
extern "C"
{
#include "lis.h"
#include "lis_config.h"
LIS_MATRIX A;
}
using namespace std;
int main(LIS_INT argc, char* argv[])
{
lis_initialize(&argc, &argv);
lis_matrix_create(LIS_COMM_WORLD, &A);
getchar();
return 0;
}
When I run this code, it gives me an access violation at the line lis_matrix_create. It seems as though A has an memory address, its data members (LIS_MATRIX is defined as a struct in Lis.h) have not been initialized, and therefore their addresses are NULL.
Could you please tell me how to create the LIS_MATRIX in such a way that I can use it like it is done in the example code on the wikipedia page?
Thank you in advance!
In reply to Adam and Ross Ridge:
I use visual studio 2013 on Windows 7 64 bit. The manual of the Lis library states that it is compatible with the Visual Studio 2008, 2010 and 2012 compilers, and also with gcc 3.4 and 4.4 and some IBM, Intel and PGI C++ compilers, I hope Visual Studio 2013 will not be a problem.
Also, in this code, if I take out the 'extern C' block, and include 'stdio.h' instead of iostream, it runs without problems (so I guess that it means the C compiler is used?). The minute I also include iostream, the access violation start.
You are including
lis_config.h
after
lis.h
wich is per se an error(you have to include it before). Also if you touched anything in lis_config you have to rebuild the whole library (using most same compilers flag of your project, for example "-msee2" if you used SSE2). Before rebuilding just swap headers only to see if that is enough..
A few more words: a library can easily detect headers included in wrong order, make a ticket to lis developers for that.

DLL built by vc6 turn to error when using by exe built by vs2012

I have a exe built by vs2012, which can successfully run with linking to dll built by vs2012, while the following error appeared when linking to the same dll built by vc6.
(Expression:_CrtIsValidHeapPointer(pUserData))
the only difference between the dll projects of vc6 and vs2012 is that the vc6 one fail to #include "SDKDDKVer.h"
Here is the code for export function of the dll
extern "C" _declspec(dllexport) RobotAI_Interface* Export()
{
return (RobotAI_Interface*)new RobotAI();
}
extern "C" _declspec(dllexport) void FreeRobotAIPointer(RobotAI_Interface* p)
{
delete p;
}
The program breaks at this specific point when debuging (a system code):
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(const void * pUserData)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) );
->}
The arrow is where it breaks.
The following is my code where it breaks:
void CArmorClientDlg::AddToList(CString file_path_name)
{
pAIManager->Add(file_path_name);
string dll_name=pAIManager->GetAI_RobotName(pAIManager->GetAINum()-1);
CListBox* pList=static_cast<CListBox*>(GetDlgItem(IDC_LIST_AI));
pList->AddString((CString)dll_name.c_str());
->}
The arrow is where it breaks. All the operations related to dll are done in "Add(file_path_name)"
I know little about the dll things... How to solve the problem?
Thank you!
TIP: the reason that I have to support vc6 dll is that the project is an AI competition, dll is the AI program that built by different contastents. The majority of the students in my school may only use vc6.
To be clear, you are mixing an EXE built with VS2012 with a DLL built with VC6? If so you are mixing C runtime library versions. While this is possible, you have to be careful to not mix heap allocations. For example, if the DLL allocates memory and returns a pointer, and the EXE tries to free the pointer, that could cause the error you see.
Either compile both the DLL and EXE with the same compiler, or write a function in the DLL that frees the pointer and use that function to pass the allocated pointer back to the DLL to be freed by the C runtime that allocated it.

Visual C++ code not working in Code::Blocks

I have the following code that I am currently using to call functions from a C# Dll, which works perfectly in Visual C++.
#include <mscoree.h>
#include <stdio.h>
#pragma comment(lib, "mscoree.lib")
void Bootstrap()
{
ICLRRuntimeHost *pHost = NULL;
HRESULT hr = CorBindToRuntimeEx(L"v4.0.30319", L"wks", 0, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*)&pHost);
pHost->Start();
printf("HRESULT:%x\n", hr);
// target method MUST be static int method(string arg)
DWORD dwRet = 0;
hr = pHost->ExecuteInDefaultAppDomain(L"c:\\temp\\test.dll", L"Test.Hello", L"SayHello", L"Person!", &dwRet);
printf("HRESULT:%x\n", hr);
hr = pHost->Stop();
printf("HRESULT:%x\n", hr);
pHost->Release();
}
int main()
{
Bootstrap();
}
The problem is, when I move this into Code::Blocks (which I am more familiar with - as the little C++ I have done has been native) throws a lot of compiler errors.
The original compiler errors were because it couldn't find the header mscoree.h. I found this in the .NET SDK, so I copied it over to the mingw include directory which solved that, and then I did the same for all the other headers it couldn't find.
After copying over all the headers it then started giving a whole load of other errors, to do with the code in the headers I had just moved - nothing to do with the code below.
Why is Code::Blocks having such a hard time running this when VS runs it straight off the bat?
Thanks
Code::Blocks is a great IDE for C++ programming, but you are clearly doing Windows programming here. Though it is the same programming language, compilers are not compatible among them.
Either if you have downloaded the CodeBlocks version with the gcc compiler, or the single CodeBlocks IDE, you need to configure CodeBlocks in order to use the MS C++ compiler. In order to do that, go to Settings >> Compiler and debugger >> Toolchain executables.
Also, in the same option, look for Search directories and place there the path to the MS C++ compiler headers.
Once that is done, you will be able to compile your program.
Code::Blocks has a different compiler altogether from Visual Studio, the decoding and encoding on source code during compilation is different and cannot recognize each other though they are same programming language.

Having trouble embedding Lua for Windows install into C++ program

This is the first question I have found myself not being able to get to the bottom of using my normal googling/stack overflowing/youtubing routine.
I am trying to compile a minimal Lua program inside of a C++ environment just to ensure my environment is ready to development. The Lua language will be later used for User Interface programming for my C++ game.
First some basic information on my environment:
Windows 7 64-bit
Visual studio 2010
Lua for Windows 5.1 (latest build I could download from google code)
Here is the code I am trying to compile:
// UserInt.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"lua5.1.dll")
#include "stdafx.h"
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * ls = luaL_newstate();
return 0;
}
#endif // __LUA_INC_H__
Here is the Error I am getting:
1>UserInt.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _wmain
1>c:\users\deank\documents\visual studio 2010\Projects\UserInt\Debug\UserInt.exe : fatal error LNK1120: 1 unresolved externals
Things I have tried:
I have read about lua_open()(and several other functions) no longer being used so I tried the newstate function instead. I get the same error. This was more of a sanity check than anything. I am using 5.1 and not 5.2 so I do not think this really matters.
I have also read this thread Cannot link a minimal Lua program but it does not seem to help me because I am not running the same environment as that OP. I am on a simple windows 7 and visual studio environment.
The top pragma comment line was something I saw in yet another thread. I get the same error with or without it.
I have gone into my visual studio C++ directories area and added the lua include to the includes and the lua lib to the libraries.
So it seems like my program is seeing the .h and seeing the symbol. But for some reason it is not getting the .cpp implementation for the functions. This is why I was hoping including that .dll directly would help fix the problem, but it hasn't.
So, I feel like I have exhausted all of my options solving this on my own. I hope someone is able to help me move forward here. Lua looks like an awesome language to script in and I would like to get my environment squared away for development.
I hope it is just some silly error on my part. I believe I have provided as much information as I can. If you need more specifics I will update with info if I can provide it.
Edit1
Tried the solution in this Can't build lua a project with lua in VS2010, library issue suspected
That did not work either.
You'll need to have the library (.LIB) file and add that to VS. Use Project > Properties and go to Linker > Input and add the full .lib filename to the "Additional Dependencies" line. Note that the .LIB is different from the .DLL.
Personally, I prefer adding the source code to my project, over referencing the dynamic link library. The following procedure will let you do as such.
Download the source code ( http://www.lua.org/ftp/ ), uncompress it.
In Visual Studio, choose File > New > Project and choose Visual C++, Win32, "Win32 Console Application".
In your project in Visual Studio, add all the source code, except luac.c. Also delete the main() function out of the file that VS created for you. This is usually given the name of the project you specified with the .cpp file extension. You could just remove this file all-together from the project.
Build and Run.
This is the Lua console