going from visual studio to g++

Started by
10 comments, last by pulpfist 14 years, 10 months ago
I made a game in C++, with SDL that I want to work with on my laptop, so after finding out that SDL+ exspress VS + vista=fail I decided to use the native g++ complier on linux. I have however never used it before. the first thing I decied to do was to get a simple program working, using SDL and mulitble files before I go down the list of my .cpp, and .h files and change all the references to file locations and includes. I am using g++ -g -o test Main.cpp Printing.cpp -lSDL -lSDL_mixer where main includes the .h for Printing class. then calls the only funtion to write a string to the screen. when I just just one file it works fine, even SDL, but its like it cant see other classes main.cpp ----------------- #include "SDL/SDL.h" #include <iostream> #include <string> #include <fstream> using namespace std; Printing test; int main( int argc, char* args[] ) { string texttext = test.Get(); cout << texttext; return 0; } --------------- Printing.cpp --------------- #include <string> #include <iostream> #include <fstream> using namespace std; string Printing::Get() { return "Die"; } --------------- Printing.h --------------- #include <iostream> #include <string> #include <fstream> class Printing { public: string Get(); }; --------------- I get this Main.cpp:21: error: ‘Printing’ does not name a type Main.cpp: In function ‘int main(int, char**)’: Main.cpp:25: error: ‘test’ was not declared in this scope Printing.cpp:7: error: ‘Printing’ has not been declared
Advertisement
You don't #include "printing.h" in either of your source files, and you also don't #include <iostream> in main.cpp.
I am still geting errors (thanks for fast responce by the way)

updated;

main.cpp
-----------------
#include "SDL/SDL.h"
#include "Printing.h"
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

Printing test;

int main( int argc, char* args[] )
{
string texttext = test.Get();
cout << texttext;
return 0;
}
---------------



Printing.cpp
---------------
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

string Printing::Get()
{
return "Die";
}
---------------




Printing.h
---------------
#include <iostream>
#include <string>
#include <fstream>


class Printing
{
public:
string Get();
};
---------------



I get this
josh@josh-laptop:~$ g++ -g -o test Main.cpp Printing.cpp -lSDL -lSDL_mixer
In file included from Main.cpp:15:
Printing.h:9: error: ‘string’ does not name a type
Main.cpp: In function ‘int main(int, char**)’:
Main.cpp:26: error: ‘class Printing’ has no member named ‘Get’
Printing.cpp:7: error: ‘Printing’ has not been declared


string lives in the std namespace. You still aren't #include "Printing.h" in printing.cpp.
Specifically, since Printing.h doesn't "using namespace std;" (this is a good thing -- you don't want to do such using statements in headers), you need to fully qualify std::string:

std::string Get();
Thank you so much, this fixed it I can now start working on my game. it was so fustrating to have my game files, have alot of impovements in mind and not be able to do anything.
my main program works fine now, and compiles with no problems.

what is the simplest way to get a exe that is workable on windows from this? (idealy in such a way the SDL libraries don't need to be installed beforehand on the computer)
You should explain more exactly what you want to achieve here. Basically, if you want to create a program for the windows platform you would be better off writing it on windows.
As far as I know you can't make g++ on linux spit out an executable file that will run in windows right off the bat since linux and windows uses different executable formats. At least as default.

However, if your code consists of standard c++ with no system calls or linux speciffic libraries, you should be able to compile it on windows without any changes.

If you want executables that runs natively on both linux and windows without recompilation, you are probably better off using java or python. Or possibly C# if you are willing to hassle with mono.

When it comes to SDL, the only file your compiled program depends on is the SDL shared libraries.
On windows, this is the SDLmain.dll file. You can stick that into your programs working directory and you should be good. Typically what you do is to set up your installer package to install this file along with your own program.

On linux the shared library is called SDLmain.so or something like that. Im not sure if you can just stick it in the working directory or if you have to install it system wide.

Eiter way, the SDL shared libraries are not the same for linux and windows so there is no easy way to make the program work on both platforms without making at least two different installers.
what I want is to compile my program so i can run it in windows as well as linux.


ya I will just compile it in windows, I was hoping there was a way to make it work nativly in both linux and windows but I guess that is too much to hope for.


thanks for your help
Since Linux and windows are NOT the same thing in anything more than the name Operating System, what you want just isn't possible.

The best you can do is compile for each platform independently. This could take some work. There are a bunch of little gotcha things between different compilers. And defiantly a few gotchas when it comes to setting the up environment so things compile the same.

The second best you can do is insure your code is compatible with emulators like Wine. This means someone on Linux can run your windows EXE.

The third best you can do is insure your code is compatible with VirtualBox/VMWare/Parallels Workstation.
This means running separate software all configured with the new OS. In this case compatibility is closer to just "my windows pc vs your windows pc" compatibility issues. But means lots of limitations and lots of work for the client.

This topic is closed to new replies.

Advertisement