Sign in to follow this  
Jpike

going from visual studio to g++

Recommended Posts

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

Share this post


Link to post
Share on other sites
You don't #include "printing.h" in either of your source files, and you also don't #include <iostream> in main.cpp.

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
string lives in the std namespace. You still aren't #include "Printing.h" in printing.cpp.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
my original code was written in visual studio, taking that to linux the only things I had to change were to reference all the header files in all the .cpp files, chance string to std::string in the headers, and to make sure the picture files could be found.


would it be possible to take all my .cpp, and .h files. And move them to a visual studio project (making sure to set up the same folder structure) then compile it there for windows?

Share this post


Link to post
Share on other sites
That should not be a problem. There is a couple of things though.
By default, when you compile a visual studio project, the resulting executables will be placed in a sub directory called ./bin/debug (or ./bin/release if you are compiling in release mode)
You may have to put your external files, like the image, in this folder in order for your program to find them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this