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

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.

Related

Problem with WFDB library in C++ project (undefined reference)

I am trying to link PhysioBank data to my Qt program in Windows 7. I followed the instruction from PhysioNet website and WFDB programmer's guide to install the package in MinGW, and tried to link the library to a simple testing program as suggested in the guide (as below). However, problems show up no mater I try with either Qt creator or Dev C++.
As suggested in the guide (p. 9), I copied curl and wfdb folders and headers to the Visual C++ directory that contains "stdio.h", and include signal.c and wfdbio.c as source files. But eventually the Qt compile error message says that "inttype.h" is not found. I guessed this is because Qt uses Visual C++ compiler but not suggested gcc, and required headers are different. But when I turn to try with Dev C++, which uses gcc 4.9.2, other errors still show up, like: "undefined reference to _imp__curl_version". Is this because the compiler is still somewhat different from that used by MinGW? If my guess is right, how could my work be possibly done?
Sorry for any beginner's mistake. Thanks very much for kind reply!
#include <stdio.h>
#include <wfdb/wfdb.h>
int main(void)
{
int i;
WFDB_Sample v[2];
WFDB_Siginfo s[2];
if (isigopen("100s", s,2) < 2)
exit(1);
for (i = 1; i< 10; i++){
if (getvec(v) < 0)
break;
printf("%d\t%d\n",v[0],v[1]);
}
exit(0);
}

Unable to use graphic programming in codeblock

#include<graphics.h>
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\TC\\BGI" );
cout<<"Please Enter to continue..."<<endl;
getch();
closegraph();
return 0;
}
All other programmes are running fine on my Codeblock 13.12, but when I run a simple graphic programme compiler crashes giving pop up window that says:
"graphic.exe has stopped working window is checking solution".
It also giving a warning message on compilation:
warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
How to resolve it?It is due to turbo C.
It is due to turbo c.
Turbo c/c++ is ancient and didn't conform any standards. There are no such header files like conio.h or graphics.h for any modern c++ compilers.
Your codeblocks IDE most probably uses the GCC toolchain by default, which doesn't provide these headers.
You should lookup for more modern 3rd party libraries for handling graphics and raw keyboard input (e.g. something like SFML). The c++ standards have no notion about such stuff so far.
For Graphics Programming In C You Need To Install Codeblocks-EP Version. Download From Here http://codeblocks.codecutter.org/setup.exe
Install And Then Go File>New>Project>Winbgim project>GO>>Add Console>>NEXT . Give Project Name,location path And Click Finish. Now Again File >New>Empty File
Write Your C graphics Code>> Build And Run.
This Codeblocks has pre installed built in graphics libraries. So You need not to add anything.

C++: Why does libtiff break the console-output?

So finally I’m not able to help myself out by researching anymore. Hopefully you can help me.
I recently decided to learn C++ in the context of my bachelor-thesis: My first aim is to read the pixel-values of a tiff-image with the libtiff-library. Yet, every call of a function of the library seems to break parts of my program.
Here’s the simple “HelloWorld”-Program, it works as it should:
#include "tiffio.h"
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
// TIFF* tif = TIFFOpen("path to .tif", "r");
return 0;
}
When I uncomment the second line in main(), the code still does compile without errors (except the warning that ‘tif’ isn’t used) and I can start the program as usual. Yet nothing gets printed into the console anymore. Not “Hello” nor any errors.
Any suggestions where the error could be? The code should be alright, I guess I messed something up during the setup of the library or so. Here’s what I did:
I managed to set up eclipse (Mars, 32bit) for C++ and MinGW (32bit) on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
I created a new C++-project with the mentioned code and did the following adjustments in the project-properties:
Project->Properties->C/C++ General->Paths and Symbols->Library
Paths-> Added “D:/… /tiff-4.0.4/libtiff/.libs”
Project->Properties->C/C++ Build->MinGW C++
Linker->Miscellaneous->Set Linkerflags to “-static-libgcc
-static-libstdc++”
Project->Properties->C/C++ Build->MinGW C++ Linker->Libraries-> Set
(-L) to “D:/… /tiff-4.0.4/libtiff/.libs” and (-l) to “libtiff”
I know the .tif is a valid file as I implemented parts of my program in C#, using the LibTiff.NET library by BitMiracle.
Thank you in advance!
Edit 1: The same error occures, even if TIFF* tif = TIFFOpen("path to .tif", "r"); is never called but written down in a dead part of the code. Debugging does not work either, the program seems to be terminated (exit value 0) before a single line is executed, without any error-reports.
I had the same issue and managed to get rid of it.
I set up eclipse (Mars) for C++ and MinGW on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
After the build, I got two directories containing files:
include
tiff.h
tiffconf.h
tiffio.h
tiffio.hxx
tiffvers.h
lib
libtiff.a
libtiff.dll.a
libtiff.la
libtiffxx.a
libtiffxx.dll.a
libtiffxx.la
I also linked all include files and only the libtiff.a to my project and that solved it, ie, the other lines are now executed.
I hope, I helped with this post.

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.

Exe build by Code blocks is almost 57 times larger than the same code build by Visual studio

This code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!\n";
return 0;
}
when comiled give size 457KB in Code::Blocks with GCC 4.4.1 and only 8KB (eight) in VS2010. Both compilers optimized for size.
Anyone knows why such a difference?
This is due to the c++ standard library being linked statically by g++, whereas VS will link it dynamically. A quick check using gcc under cygwin gives me approximately same sizes, and the resulting exe only import some C functions.
#include <stdio.h>
int main() {
printf("Hello world\n");
return 0
}
On the other hand, this application compiled to the same minimal EXE under gcc, as it does not require any c++ functionality.
You are right, the executable by gcc is obviously larger, in your case 57 times larger than that built by vc++.
The main reason is the one made with
GCC won't require any external
dependencies to run while the one made
with VS2010 would need at least its runtime
files to be present on the system.
For instance, say you try it on some friend's computer with no vs2010 installed, rather try earlier OS like XP with no chance of having VS2010 runtimes even.
The one built with GCC will have no problems while the one made with VS2010 would throw an error of missing runtime file ( dependency ).
Hope this helped, if it didn't or you have any other question in your mind, feel free to ask and I'll be glad to help :)