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

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.

Related

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.

Why does "cout" keep giving me error C1083?

I was using Visual C++ 6.0 just now, and I keep getting this error:
fatal error C1083: Cannot open include file: 'streambuf': No such file or directory
My code is just a simple hello world program.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
cout<<"Hello World.";
return 1;
}
Then I went and checked my INCLUDE folder and found a file called STREAMBF, but the compiler was looking for STREAMBUF. Notice that the file that is present is missing the U, between the B and the F. This was from a good copy of VC++6.0, directly from the actual CD, not a pirated copy. So there should be all the files needed. But it appears that a file is missing! Is this MS being stupid again, and yet making another big mistake, and forgetting to include an important file on their CDs? I'd hate to think that every single CD for VC++6.0 that was pressed that came out of MS factories had this problem. And I know that it is a missing file, not just a misnamed file, as renaming STREAMBF to STREAMBUF just led to more errors.
Anybody know where I can find a copy of the file STREAMBUF? Or am I just overlooking something here? Is this exact error a known problem with running old copies of VC++ on modern OS's like Windows 7? Is it possible that the only reason that it's looking for STREAMBUF is that this is a newer file associated with Win7, and that if it was running in a different environment (an older OS), it would actually be looking for the correct file, STREAMBF? Can somebody help me here?
Your installation is either broken, deprecated or interpretes your code in wrong way.
You should only use older compiles if you are trying to build project developed entirely for this version.
Try to compile same code with new compiler, if you want to use VS then you should look for Visual Studio Express 2013.
Your code does not have any errors.
Modify your program to, you should be able to see it okay.
#include <iostream.h>
using namespace std;
int main()
{
cout<<"Hello World.";
return 1;
}
However,
your compiler is pretty old. You need to an upgrade.
There are C++ compilers for Windows from Microsoft Express Visual Studios Link and Info VS2013 to
some other non-Microsoft like GCC for Windows.
If you don't have installation access there are some portable c++ compilers.
Finally there are some online compilers for simple test. web based online compilers.
For my win 10 installation of VC 6.0, I had the same problem ... fatal error C1083: Cannot open include file: 'streambuf': No such file or directory
Replacing with <iostream.h> does not solve the problem.
I have checked the header file installation folder (Program files\VS98\VC98\INCLUDE). For some (unknown) reason, some file names have been changed during installation. Restoring the original name has solved the problem, in my case, in example:
Turn STREAMBF into STREAMBUF, STDXCEPT into STDEXCEPT, XCEPTION into EXCEPTION, FCTIONAL into FUNCTIONAL.
Notice: other header file names might be wrong. I have listed above the file names wrong in my installation.
I hope this may help.

c++ And lua-how to begin?

im trying out other languages.
Got VB2013 and LuaForWindows 5.1 What is the most basic file structure to run a .lua file in my program??
I have currently done http://www.youtube.com/watch?v=w51pftzS1_8 includes part, made a .h file that looks like this
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#endif // __LUA_INC_H__
and a Run.cpp like this
#include <iostream>
#include <conio.h>
#include <iostream>
#include "LuaInc.h"
using namespace std;
int main()
{
int iErr = 0;
lua_State *lua = lua_open(); // Open Lua
luaopen_io(lua); // Load io library
if ((iErr = luaL_loadfile(lua, "test.lua")) == 0)
{
// Call main...
if ((iErr = lua_pcall(lua, 0, LUA_MULTRET, 0)) == 0)
{
// Push the function name onto the stack
lua_pushstring(lua, "helloWorld");
// Function is located in the Global Table
lua_gettable(lua, LUA_GLOBALSINDEX);
lua_pcall(lua, 0, 0, 0);
}
}
lua_close(lua);
_getch();
return 0;
}
the test.lua file is in the vb213 projects dir/MYPROJECT/MYPROJECT
and looks like this
function helloWorld ()
io.write ("hello World")
end
From VS2013 and Lua for Windows, you are going to have some pain related to getting the right C Runtime Library version in play. Lua for Windows was compiled against an older version of the CRT, that came with VS2005. It may or may not be possible to get VS2013 to link against that older version. Mixing CRT versions is a recipe for much confusion.
The easiest way out is to get a version of the Lua core built for your version of Visual Studio. There are two ways to do that.
Download a version from Lua Binaries. There are pre-built versions of Lua available from the "official" Lua Binaries distribution. It can be had for 32-bit and 64-bit builds, for Windows, and for other platforms.
Build Lua yourself as part of your solution. Building your own Lua51.dll is straightforward, the default configuration is sensible. It mostly amounts to including almost all the .c files in a DLL project. Note that lua.c is not part of the DLL, that is the source to lua.exe. Similarly, luac.c is not part of the DLL, it is the source to luac.exe which does require some care to build yourself; but you aren't likely to need it.
Either way, you need to pay attention to some details.
The Lua API is a C API, not C++. So if you insist on making your application be a C++ application, you should wrap the inclusion of the Lua header files inside of an exern "C" block:
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
In principle, you can build the Lua core as C++ instead. The core is written in a clean C flavor that is also a subset of C++, and is tested when compiled as C++. However, if you go that route then you must build any binary modules yourself against your C++ linkage, and that way can lie madness if you depend on many community supplied modules.
I believe that all of the builds available at Lua Binaries are compiled as C, not C++, so the extern "C" declaration will be required with those.
Well, for starters, I would recommend scratching c++ and switching to lua as your primary programming language. It is quite messy to call lua functions from with c++, at least that is my experience. Once you understand enough of Lua, THEN call c/c++ functions from Lua via dll or other file/system formats. I was torn between learning python or c/c++ and I chose python. From there I learned about Lua and loved it more than python. Again this is just my experience and suggestion. Now to answer your question, why run Lua in C++? Lua is based on the C API, so I would just use raw C or just stick with C++. I say this only because Lua is technically no different than a watered down version of C. However, Lua is not "watered down" any more, as it can be used as a fully operational standalone programming language.

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 :)

Borland Can't Compile, what is going on, I can't even get started

C:\BORLAND\BCC55\BIN>bcc32 hello.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
hello.cpp:
Error E2209 hello.cpp 2: Unable to open include file 'iostream'
Error E2090 hello.cpp 6: Qualifier 'std' is not a class or namespace name in fun
ction main()
Error E2379 hello.cpp 6: Statement missing ; in function main()
*** 3 errors in Compile ***
I'm veeery sad, you see! :-(
#oggy: I read the instructions at Embarcadero. Now, it says...
#include <iostream.h>
int main(void)
{
cout << "Hello." << endl;
return 0;
}
C:\Borland\BCC55\Bin\MySource>bcc32 hello.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
hello.cpp:
Error E2209 hello.cpp 1: Unable to open include file 'iostream.h'
Error E2451 hello.cpp 4: Undefined symbol 'cout' in function main()
Error E2451 hello.cpp 4: Undefined symbol 'end' in function main()
Error E2379 hello.cpp 4: Statement missing ; in function main()
*** 4 errors in Compile ***
Seriously, you're going to keep having troubles if you continue to use Borland's compiler. It's free from their computer museum for a reason - it's ancient. The copyright line itself should be proof enough of that:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Do you really want a compiler that hasn't been updated in decades, one that the standard has long since left behind?
It's the compiler you get if you're interested in vintage computing stuff, in the same league as people with TRS-80 and Apple II emulators :-)
Download Microsoft Visual C++ Express and install it. It's as free (as in cost) as the Borland one and substantially more up to date. See here for the product page.
Or there are many other more up-to-date development tools you can get for free as well, such as gcc, Code::Blocks and so forth.
The first error would suggest that you didn't bother to read the installation instructions.
If you want to stick to Borland products you can install Turbo C++. I'm talking about the 2006 Turbo C++ part of the "Turbo Explorer" effort to bring back some of the popularity of the 90's Turbo C++.
They brag with "Turbo C++ contains support for the industry standard ANSI C and ISO/ANSI C++ languages and libraries. Turbo C++ also includes the Dinkumware C++ runtime libraries and support for the popular Boost library."
I think that a 2006 implementation should be decent enough, somehow not so popular like Visual Studio Express 2005/2008.
Regarding the compilation problems, one must fiddle with the two configuration files found in the bin directory (in this case C:\BORLAND\BCC55\BIN), namely bcc32.cfg and ilink32.cfg. The compiler cannot find the iostream.h file.
**create two file inside C:\Borland\bcc55\bin
edit its info with following**
in file BCC32.cfg
-I"c:\Borland\Bcc55\include"
-L"c:\Borland\Bcc55\lib"
Create another file with name ILINK32.cfg
-L"c:\Borland\Bcc55\lib"
now use your compiler and don't forget to add
#include<iostream>
using namespace std;
at heading section.
"iostream.h" is not a standard c++ header, some compilers provide it for legacy support, but you should always use just "iostream" instead. The main difference between the legacy and the standard one is the std namespace. To have a compliant version of your example, it would look like this:
#include <iostream>
using namespace std; // import the contents of the std namespace
// into the global namespace
int main() {
cout << "Hello." << endl;
return 0;
}