Allegro4/C++ giving error - c++

I'm using the following code (Allegro 4, C++), and getting the following error:
#include <allegro.h>
//defines
#define MODE GFX_SAFE
#define WIDTH 640
#define HEIGHT 480
int main (void)
{
int ret;
int counter;
//initialize allegro
allegro_init();
install_keyboard();
install_timer();
srand(time(NULL));
//set up screen
//set video mode
ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
if (ret != 0)
allegro_message(allegro_error);
allegro_exit();
return 0;
}
Error:
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
All the previous answers regarding that error tell me to switch to "Console" from "Windows"; but I already have "Console" in Properties->Linker->System->Subsystem.
If you don't have an answer, I'd be happy with something I could do to help narrow down the problem: I've used Allegro with C, but I want to use C++ to take advantage of OOP, and so I still have a lot of work to do.
Update:
#include <iostream>
#include <allegro.h>
using namespace std;
int main ()
{
cout << "Hello World";
return 0;
}
doesn't work, but
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World";
return 0;
}
does.
Now what? Answer: Start with Empty project.
Update2: restarted with an empty project, same code. First block (alleg.lib in linker, but allegro.h not included) works, second code (allegro.h included) doesn't. However, the bug is different:
1>LINK : fatal error LNK1561: entry point must be defined
What now?
Edit^2:Ignore all the following: I forgot to go back to including Allegro. It works now. Thanks everyone for the answers.
Edit: Adding:
END_OF_MAIN()
or
int END_OF_MAIN()
give the error "fatal error C1004: unexpected end-of-file found"

You are getting the error because you are attempting to integrate allegro into a project that is non-empty.
You must create the project as an EMPTY PROJECT type:
New... > Project... > Visual C++ > Empty Project
--EDIT FOR SECOND ERROR--
You must append END_OF_MAIN() after the closing brace of int main():
int main() {
//...
}
END_OF_MAIN()

Related

Linking error in OpenCV programs when using third party libraries

I am trying to use graph library using opencv.
This is the code that I have written , and am building this on Visual studio 2010.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "GraphUtils.h"
using namespace cv;
using namespace std;
int main()
{
float floatArray[4]= {1,1,2,2};
showFloatGraph("Rotation Angle", floatArray, 4 );
return 0;
}
There is no compilation error.
However, I am getting following linking error:
Error 1 error LNK2019: unresolved external symbol _showFloatGraph
referenced in function _main C:\Users\Yam\Documents\Visual Studio
2010\Projects\plot\opencv1\helloworld.obj
Error 2 error LNK1120: 1 unresolved externals C:\Users\Yam\Documents\Visual Studio
2010\Projects\plot\Debug\opencv1.exe
I have put the two files (GraphUtils.h and GraphUtils.c) with my .cpp file.
Why I am getting these compilation errors whenever I am using third party libraries (.c and .h) files, since I get this linking error also when using other library. I asked the question here
In Properties=>Linker=>input=>Additional dependencies , I have added :
opencv_core231d.lib;opencv_highgui231d.lib;opencv_imgproc231d.lib;opencv_features2d231d.lib;opencv_calib3d231d.lib;%(AdditionalDependencies)
And in fact my basic opencv program for image display works fine.
Do I need to do something special when using these libraries. As of now I just copy paste the .c and .h files in the folder where my .cpp file is present. Do I need to do something more ?
Update:
I found that the the file GraphUtils.c has the following code:
// OpenCV
#include <cv.h>
#include <cxcore.h>
#ifdef USE_HIGHGUI
#include <highgui.h>
#endif
I found that the cv.h, cxcore.h and highgui.h are actually present in opencv 2.3.1/opencv folder and so I changed it like this:
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#ifdef USE_HIGHGUI
#include <opencv/highgui.h>
#endif
Unfortunately this gave rise to more linking errors.
Update
The following program works fine.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include<conio.h>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("xyz.bmp", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
getch();
return 0;
}
But this program always gives error; no header file recognized. so no function would work:
#include "cv.h" //main OpenCV header
#include "highgui.h" //GUI header
int main() {
// declare a new IplImage pointer
IplImage* myimage;
// load an image
myimage = cvLoadImage("sayyidsmile.jpg",1); //change the file name with your own image
//create a new window & display the image
cvNamedWindow("Smile", 1);
cvMoveWindow("Smile", 100, 100);
cvShowImage("Smile", myimage);
//wait for key to close the window
cvWaitKey(0);
cvDestroyWindow( "Smile" );
cvReleaseImage( &myimage );
return 0;
}
Details on Env. Variables
I followed this article to set EVs.
Currently my variables are like this:
Name Value
OpenCV: C:\Users\Yuvue\Desktop\OpenCV2.3.1
INCLUDE: %OPENCV%\build\include
LIB: %OPENCV%\build\x86\vc10\lib
Path: %OPENCV%\build\x86\vc10\bin
Under Properties => VC++ Directories=> Library Directories, I have added the following:
$(OPENCV)\build\x86\vc10\lib
The second program compiles when I explicitly gives path of cv.h in the include. But if I do so with any of the third party libraries when they have used #include, it starts giving linking error (my original question).
Please help me sort this!!

C++ Anti-aliased line for beginner to SDL

Original question:
I've been asked prior to a job interview to understand how an
anti-aliased line is drawn in a framebuffer, using C or C++. I haven't
used C, and it's been a few years for me since last using C++. I am a
complete beginner when it comes to graphics. My C++ experience has
mostly been in simple command-line programs and sorting methods. The
company does not care if I grab the code online, they want me to
understand it but still have a working executable.
I've used this tutorial to set up SDL libraries in MS VC++ 2012
Express, and this algorithm for the actual anti-aliasing. I have
a good understanding of the algorithm, though I'm currently having
trouble getting it to compile. I just want a line to be drawn, and
then I can go forward with setting the code up to the skeleton class
definitions I was given. This is what I have included aside from what
is on that page with the algorithm:
#include <cmath>
#include <math.h>
#include "conio.h"
#include "stdlib.h"
#include "stdio.h"
#include "SDL.h"
const double HEIGHT = 240;
const double WIDTH = 320;
const double X0 = 25.6;
const double X1 = 64.7;
const double Y0 = 30;
const double Y1 = 42;
int round(double number)
{
return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);
}
void main()
{
Uint32 pixelColor = 00000000000000000000000000000000;
SDL_Surface* myScreen = SDL_CreateRGBSurface(SDL_ALPHA_OPAQUE,WIDTH,HEIGHT,32, 0x000000FF,
0x0000FF00, 0x00FF0000, 0xFF000000);
WULinesAlpha(X0, X1, Y0, Y1,pixelColor,myScreen);
return;
}
I'm getting the following errors:
Error 21 error LNK2019: unresolved external symbol _SDL_main
referenced in function _main Error 22 error LNK1120: 1 unresolved
externals
I've seen a few code examples saying the main function has to look
like this:
int main(int argc, char *argv[])
{
}
Again, graphics stuff is unfamiliar to me so I know my main function
is likely very wrong; I'm anticipating some shaking heads. Can someone
explain what is happening/what I need to do?
New:
I have now replaced my main function with the following code, based on NomNomNom069's YouTube video: "C++ SDL Tutorial 2 Creating a Screen and Handling Basic Input"
#include "SDL.h"
int main(int argc, char * args[])
{
bool running = true;
//initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
running = false;
}
//set up screen
SDL_Surface *screen;
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE);
if (screen == NULL)
{
running = false;
}
SDL_Event occur;
//main application loop
while (running)
{
SDL_PollEvent(&occur);
if (occur.type == SDL_QUIT)
{
running = false;
}
//drawing occurs here
SDL_FillRect(screen, NULL, 0);
SDL_Flip(screen);
}
//quit SDL
SDL_Quit();
return 0;
}
No errors, and I get a window to pop up. Awesome.
My question now is regarding how/where to call WuLinesAlpha. This function calls for 4 doubles, a Uint32 variable, and an SDL_Surface*. I have my doubles, I set the Uint32 to 0x000000FF, and I assume that the SDL_Surface I have set up as screen is the one passed in.
I've toyed around with where the WuLinesAlpha function call goes and I keep getting the black screen. I thought, as explained in the video, it would go in the loop but nothing has happened. Are there any more SDL commands I should be calling?
Fix your main declaration first. This does need to be int main(int argc, char *argv[]). Especially on Windows, since I believe SDL.h actually renames your main to some other name, and takes over main for the library itself.
Next, make sure you link against SDL properly. In my own SDL 1.2.x based project I have these lines in my Makefile:
SDL_CFLAGS := $(shell sdl-config --cflags)
SDL_LFLAGS := $(shell sdl-config --libs)
I then later append those flags to my actual CFLAGS and LFLAGS. Note that if you use make and Makefiles, you want to use := there, otherwise make will invoke the $(shell ...) command every time it expands $(CFLAGS).
I can't help you set up Microsoft's GUI products. This tutorial, for a slightly older MSVC product (2010), looks pretty good, and may put you on the right track: http://lazyfoo.net/SDL_tutorials/lesson01/windows/msvsnet2010e/index.php
And finally, don't forget to call SDL_Init() at some point, preferably before you start creating surfaces.
Good luck!

Octave c++ and VS2010

I'm trying to Use Octave with Visual C++.
I have downloaded octave-3.6.1-vs2010-setup-1.exe. Created a new project, added octave include folder to include path, octinterp.lib and octave.lib to lib path, and I added Octave bin folder as running directory.
The program compiles and runs fine except feval function that causes the exception:
Microsoft C++ exception: octave_execution_exception at memory location 0x0012faef
and on Octave side:
Invalid resizing operation or ambiguous assignment to an out-of-bounds array element.
What am I doing wrong?
Code for a standalone program:
#include <octave/octave.h>
#include <octave/oct.h>
#include <octave/parse.h>
int main(int argc, char **argv)
{
if (octave_main (argc, argv, true))
{
ColumnVector NumRands(2);
NumRands(0) = 10;
NumRands(1) = 1;
octave_value_list f_arg, f_ret;
f_arg(0) = octave_value(NumRands);
f_ret = feval("rand",f_arg,1);
Matrix unis(f_ret(0).matrix_value());
}
else
{
error ("Octave interpreter initialization failed");
}
return 0;
}
Thanks in advance.
I tried it myself, and the problem seems to originate from the feval line.
Now I don't have an explanation as to why, but the problem was solved by simply switching to the "Release" configuration instead of the "Debug" configuration.
I am using the Octave3.6.1_vs2010 build, with VS2010 on WinXP.
Here is the code I tested:
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
int main(int argc, char **argv)
{
// Init Octave interpreter
if (!octave_main(argc, argv, true)) {
error("Octave interpreter initialization failed");
}
// x = rand(10,1)
ColumnVector sz(2);
sz(0) = 10; sz(1) = 1;
octave_value_list in = octave_value(sz);
octave_value_list out = feval("rand", in, 1);
// print random numbers
if (!error_state && out.length () > 0) {
Matrix x( out(0).matrix_value() );
std::cout << "x = \n" << x << std::endl;
}
return 0;
}
with an output:
x =
0.165897
0.0239711
0.957456
0.830028
0.859441
0.513797
0.870601
0.0643697
0.0605021
0.153486
I'd guess that its actually stopped pointing at the next line and the error actually lies at this line:
f_arg(0) = octave_value(NumRands);
You seem to be attempting to get a value (which value?) from a vector and then assigning it to element 0 of a vector that has not been defined as a vector.
I don't really know though ... I've never tried writing octave code like that. I'm just trying to work it out by translating the code to standard matlab/octave code and that line seems really odd to me ...

LNK2019: Error. unresolved external symbol in C++ program using InternetOpen InternetReadFIle

I have tried writing a simple program to get information from a website. I can't compile as I get the LNK2019 error for InternetReadFile, InternetOpenUrl, etc. and e.g.
1>GetInternetInfo.obj : error LNK2019: unresolved external symbol _imp_InternetReadFile#16 referenced in function _main
I assume that means I did not define these functions, that I did not include the correct library. I thought including #include would fix it, but it does not seem to help. I am running this on Visual Studio 2010 using C++. Below is my program. Any help is appreciated.
#include <string>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <wininet.h>
#include <winsock.h>
#include <stdio.h>
#include <stdarg.h>
using namespace std;
int main()
{
HINTERNET hOpen, hURL;
LPCWSTR NameProgram = L"Webreader"; // LPCWSTR == Long Pointer to Const Wide String
LPCWSTR Website;
char file[101];
unsigned long read;
//Always need to establish the internet connection with this funcion.
if ( !(hOpen = InternetOpen(NameProgram, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 )))
{
cerr << "Error in opening internet" << endl;
return 0;
}
Website = L"http://www.google.com";
hURL = InternetOpenUrl( hOpen, Website, NULL, 0, 0, 0 ); //Need to open the URL
InternetReadFile(hURL, file, 100, &read);
while (read == 100)
{
InternetReadFile(hURL, file, 100, &read);
file[read] = '\0';
cout << file;
}
cout << endl;
InternetCloseHandle(hURL);
return 0;
}
Please include "Wininet.lib" in your project settings.
Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies
You can also add this line to your code after include section instead of adding library to the properties:
#pragma comment(lib, "wininet.lib")
Did you link to wininet.lib?
http://msdn.microsoft.com/en-us/library/aa385103(VS.85).aspx

Boost::Test -- generation of Main()?

I'm a bit confused on setting up the boost test library. Here is my code:
#include "stdafx.h"
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE pevUnitTest
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE( TesterTest )
{
BOOST_CHECK(true);
}
My compiler generates the wonderfully useful error message:
1>MSVCRTD.lib(wcrtexe.obj) : error LNK2019: unresolved external symbol _wmain referenced in function ___tmainCRTStartup
1>C:\Users\Billy\Documents\Visual Studio 10\Projects\pevFind\Debug\pevUnitTest.exe : fatal error LNK1120: 1 unresolved externals
It seems that the Boost::Test library is not generating a main() function -- I was under the impression it does this whenever BOOST_TEST_MODULE is defined. But ... the linker error continues.
Any ideas?
Billy3
EDIT: Here's my code to work around the bug described in the correct answer below:
#include "stdafx.h"
#define BOOST_TEST_MODULE pevUnitTests
#ifndef _UNICODE
#define BOOST_TEST_MAIN
#endif
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#ifdef _UNICODE
int _tmain(int argc, wchar_t * argv[])
{
char ** utf8Lines;
int returnValue;
//Allocate enough pointers to hold the # of command items (+1 for a null line on the end)
utf8Lines = new char* [argc + 1];
//Put the null line on the end (Ansi stuff...)
utf8Lines[argc] = new char[1];
utf8Lines[argc][0] = NULL;
//Convert commands into UTF8 for non wide character supporting boost library
for(unsigned int idx = 0; idx < argc; idx++)
{
int convertedLength;
convertedLength = WideCharToMultiByte(CP_UTF8, NULL, argv[idx], -1, NULL, NULL, NULL, NULL);
if (convertedLength == 0)
return GetLastError();
utf8Lines[idx] = new char[convertedLength]; // WideCharToMultiByte handles null term issues
WideCharToMultiByte(CP_UTF8, NULL, argv[idx], -1, utf8Lines[idx], convertedLength, NULL, NULL);
}
//From boost::test's main()
returnValue = ::boost::unit_test::unit_test_main( &init_unit_test, argc, utf8Lines );
//End from boost::test's main()
//Clean up our mess
for(unsigned int idx = 0; idx < argc + 1; idx++)
delete [] utf8Lines[idx];
delete [] utf8Lines;
return returnValue;
}
#endif
BOOST_AUTO_TEST_CASE( TesterTest )
{
BOOST_CHECK(false);
}
Hope that's helpful to someone.
Billy3
I think the problem is that you're using the VC10 beta.
It has a fun little bug where, when Unicode is enabled, it requires the entry point to be wmain, not main. (Older versions allowed you to use both wmain and main in those cases).
Of course this will be fixed in the next beta, but until then, well, it's a problem. :)
You can either downgrade to VC9, disable Unicode, or try manually setting the entry point to main in project properties.
Another thing that might work is if you define your own wmain stub, which calls main. I'm pretty sure this is technically undefined behavior, but as a workaround for a compiler bug in an unreleased compiler it might do the trick.