2

First of all, I am a beginner programmer so, please give simple answers that are understandable.

I am suing Code::Blocks and I am trying to make a multiple file project in C++. I am using SDL2. My problem is that when I put all my codes and functions altogether, they run and when I seperate them, they don't. This is because the execution file requires object files to be built which it don't get. It don't get them because compiler don't form them. In other words, there are problems with linking. The compiler simply, said that there are not ".o" files. These files are in the project as "Link Files" and not "Compile Files". And when I make them "Compile Files" as well as "Link Files", they produces errors that various variables inside the file where I mentioned them, are not declared. But when I include that file in the file using the variables, it gets deeply nested.

Following are the two ways:

First way which is not working, with seperate files.

In main.cpp:

    #include <SDL.h>
    #include <other.h>

    int var;
    int linkVar;
    int link1Var;
    int link2Var;

    #include "link.cpp"
    #include "link1.cpp"
    #include "link2.cpp"

    int main( int argc, char* args[] )
    {
      linkFunc();
      link1Func();
      link2Func();
    }

In link.cpp/ link1.cpp/ link2.cpp:

    void linkFunc()/void link1Func()/void link2Func() //Just a reference
    {
      //Code associated with var, link1Var, link2Var and link3Var.
    }

Second way which is working, with all functions in a single file.

First thing is that I don't want to learn makefile thing. If there is any other way to solve it then be it!

Clifford
  • 88,407
  • 13
  • 85
  • 165
user3820248
  • 84
  • 2
  • 10

2 Answers2

1

Code::Blocks makes object files for each file that it compiles as separate compilation units. It will compile and link together any files that are part of the project which it identifies as a source file.

First, remove your #includes of the cpp files. Then try the Project > Add files... menu option to add those other cpp files to the project.

Jonny D
  • 2,244
  • 15
  • 22
0

First of all, you should not use #include on other cpp files, you can read about it here: include cpp

Secondly when you are trying to compile a project composed of separate files you should use the g++ command like this: g++ main.cpp link.cpp link2.cpp etc' you can read a basic explanation about it here: use the g++ command

To do exactly what you asked you should do:

# get rid of including any cpp files

# use forward declerations on any function belonging in other cpp files, before your main function like this:

void linkfunc();
void linkfunc2();
etc
.
.
. 
int main()
{
}

# specify the direct path to your .h files like this:

#include <C:/MinGW/SDL2/SDL.h>

# use this command:

g++ -I C:/MinGW/SDL2/ main.cpp link.cpp link2.cpp link3.cpp
Community
  • 1
  • 1
Ravid Goldenberg
  • 2,119
  • 4
  • 39
  • 59
  • I understand that #include thing. Isn't g++ associated with makefile thing? – user3820248 Dec 07 '14 at 14:21
  • @user3820248 how are you compling your program? – Ravid Goldenberg Dec 07 '14 at 15:28
  • @user3820248 You got it the other way around, make-file is associated with g++(and others..) command, I strongly recommend you read this even if you don't want to use make-file: http://www.sis.pitt.edu/~mbsclass/tutorial/advanced/makefile/whatis.htm – Ravid Goldenberg Dec 07 '14 at 15:33
  • Let me clarify what I wanted to do. I want to know the simplest method of compiling multiple files to form an execution file, altogether. For example, for the above files, what should one do to compile? – user3820248 Dec 08 '14 at 14:13
  • How is this makefile, wrong?`% g++ -c -o main.o main.cpp % g++ -c -o link.o link.cpp % g++ link2.o link2.cpp % g++ -o exec main.o link.o link2.o -L C:/MinGW/SDL2` Problem: *** recipe commences before first target. Stop. – user3820248 Dec 08 '14 at 14:28
  • I have edited my anser, it now contains the simpest sulotion. – Ravid Goldenberg Dec 08 '14 at 16:08
  • Writing this command at the end of main.cpp produces problem! Where to write `g++ -I C:/MinGW/SDL2/ main.cpp link.cpp link2.cpp link3.cpp`? – user3820248 Dec 09 '14 at 14:31
  • @user3820248 I ask again, how are you compling this? are you using visual studio? that command is ment for running you progrma from the terminal in unix. – Ravid Goldenberg Dec 09 '14 at 17:03
  • Oh! Now, where is that Unix terminal? I am compiling in Code::Blocks and I already, had mentioned this at the top. The compiling is good (program runs when all gets in a file) but linking is my problem. – user3820248 Dec 11 '14 at 14:12
  • When I googled that how can I open unix terminal in windows, I found Cygwin but I don't know what is it! If you know something about Cygwin then, please tell me. Thanks in advance! – user3820248 Dec 11 '14 at 14:16
  • @user3820248 budy if you are using windows, i assume you are using visual studio in which case you need no make file and no commands in the terminal, simply # get rid of including any cpp files # use forward declarations on any function belonging in other cpp files, before your main function and run your program just the way you run it as if all the code is in one file. – Ravid Goldenberg Dec 11 '14 at 14:25
  • Sorry to say but thats not my fault what you assume! I already, had mentioned that I am using Code::Blocks in the passage that I typed above. I understand every thing you said but the point is "Files are still not connected!". – user3820248 Dec 11 '14 at 14:55
  • are you using visual studio? are all the files in the same project? – Ravid Goldenberg Dec 11 '14 at 14:57
  • I am not using Visual Studio. All my files are in the same profect and are categorized as "Link Files". I also tried them as "Compile Files" or both. But all in vain! – user3820248 Dec 11 '14 at 15:27
  • How are you compiling your program if all the code is in one file? – Ravid Goldenberg Dec 11 '14 at 17:31
  • Where you said to declare all functions, I put all the functions' definations in the main.cpp. And it works but all code is in one file. – user3820248 Dec 12 '14 at 08:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66724/discussion-between-petric-and-user3820248). – Ravid Goldenberg Dec 12 '14 at 08:52