• Lucid Dreaming - Dream Views




    Results 1 to 25 of 102
    Like Tree3Likes

    Thread: Ask me about C / C++

    Hybrid View

    1. #1
      Motion Personified Alpha Achievements:
      Referrer Bronze 1000 Hall Points Veteran First Class
      mrdeano's Avatar
      Join Date
      Apr 2009
      Gender
      Location
      United Kingdom
      Posts
      1,053
      Likes
      58
      DJ Entries
      1
      Hey, I am trying to cover the basics of C++.
      I think I have some of them down, I just need to double check if I'm right.

      Code:
      #include "stdafx.h"
      #include <iostream>
      
      using namespace std;
      
      int main()
      {
          cout << "Here is some text";
          cout << endl;
      
          cout << "What is 15 + 17? the answer is:";
          cout << 15 + 17;
      
          cin.get();
          return 0;
      }
      Above is the first code I have written for C++, I found a little beginners tutorial for it. It is important to understand the code, so here it goes.

      Code:
      #include "stdafx.h"
      #include <iostream>
      They tell the compiler to include these in the .exe
      They are both separate files containing code.
      stdafx.h is a header code and isostream contains different packages that do several things.

      Code:
      using namespace std;
      Tells the computer that it wants to use the package 'std' from ;isostream

      Code:
      int main()
      Is the main function which every piece of code should have.

      Code:
      {
      Opens the code.

      Code:
      cout << "Here is some text";
      'cout <<' gives the program a output, or something to print. In this case its a line of text.
      Cout is a keyword taken from the package 'std' which is in 'iostream;

      Code:
      cout << endl;
      'endl' ends the line

      Code:
      cout << 15 + 17;
      This tells the computer to print the answer of this calculation.

      Code:
      cin.get();
      This makes the program wait for an input by the user

      Code:
      return 0;
      Once it has the input, it tells the computer to revert back to what it was doing before hand

      Code:
      }
      closes code




      I was wondering if you could check if I have it all right.
      If there's anything I am missing as a beginner.. I would appreciate it if you point it out


      thanks

    2. #2
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by A Roxxor View Post
      Code:
      using namespace std;


      Quote Originally Posted by mrdeano View Post
      Code:
      #include "stdafx.h"
      #include <iostream>
      They tell the compiler to include these in the .exe
      They are both separate files containing code.
      stdafx.h is a header code and isostream contains different packages that do several things.
      Yes.

      Code:
      using namespace std;
      Tells the computer that it wants to use the package 'std' from ;isostream
      ... kind of... That tells it to use the std namespace throughout the program. It is considered better programming technique to only have a namespace over small scopes. It actually allows you to transcend scope, this is very useful in large programs so that you don't have duplicate variables and have to worry about concurrency.

      Code:
      int main()
      Is the main function which every piece of code should have.
      Yes, it's the entry point of the operating system, there is more to it, but you don't need to know that unless you are doing assembly optimizations.

      Code:
      cout << "Here is some text";
      'cout <<' gives the program a output, or something to print. In this case its a line of text.
      Cout is a keyword taken from the package 'std' which is in 'iostream;

      Code:
      cout << endl;
      'endl' ends the line
      Yes, cout is a stream that goes to the terminal, the real thing that you need to take away from that line is the << operator. There are lots of functions that use it, you can create classes and use STL libraries to send strings through pipes, files, even the internet.

      endl is a std const for cross compatibility, most programers use \n instead though, even though we shouldn't

      Code:
      cin.get();
      This makes the program wait for an input by the user
      Yes, more specifically keyboard input, inputs from other devices won't trigger this method.

      Code:
      return 0;
      Once it has the input, it tells the computer to revert back to what it was doing before hand
      Kind of, it tells the operating system to close the program and return an error code of 0. Returning things other than zero from the main loop tells the operating system that something went wrong, and depending on the code and the OS, it may attempt some type of recovery. From -127 to -1, the system may try a recovery, from 1 to 127, it is a user-defined error and will not produce any type of recovery on the OS level, it may be read by a calling or forking program though.

    3. #3
      Member
      Join Date
      May 2009
      Posts
      139
      Likes
      1
      I have a package called "atomic parsley". Instead of a make file, it has a shell script with commands to compile each unit without linking, then goes through a second time and links everything. Each source file seems to have all the includes it needs, as-is. But on the compile w/o linking stage, it's using g++ with command line options which I don't know the importance of, which fail with a syntax error on all the c compilers I've got, even the version of gcc I have.

      Are there any quick guides to creating a proper make file for projects lacking one?

    4. #4
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Ouch, creating makefiles for large projects suck

      What options are being set that you don't know the significance of? Most likely the problem is that the program was written in Visual C++ and not GNU C++. M$ doesn't adhere to C++ standards, so their software won't work in other compilers, that's why so few people write in Visual C++ outside of M$ itself.

      The basic makeup of a makefile is setting the flags to variables, the creating a command for each file. Here's a good tutorial.

      What options were being set that you didnt' understand?

      The most common ones are:
      -c : compile, but don't link
      -O1, O2, O3, Os : optimization levels
      -strip : strip unnecessary symbols and code
      -o : send to this output file
      -l : link a library
      -I : include paths
      -g : include debugging information it the executable
      -Wall : Warnings All - warns you about everything

    5. #5
      Member
      Join Date
      May 2009
      Posts
      139
      Likes
      1
      Yes, I've been figuring that this will suck. But I really need some kind of code for dumping itunes m4a tags to a plain text file Besides the gcc that came with the linux I'm using, I've got borland c++ and symantec c++ for windows.
      I got them back when I was taking some classes in C programming.

      There is a command line in the shell script similar to this for each source file during the compiling stage-

      g++ -g -O2 -Wall -MT obj_files/AP_commons.o -MD -MP -MF
      "./obj_files/AP_commons.Tpo" -c -o obj_files/AP_commons.o
      AP_commons.cpp

      Each of those compile commands gets the following error message, and then doesn't produce any output file-

      g++: cannot specify -o with -c or -S and multiple compilations

      When the link command is run later, there's nothing to link.

    6. #6
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      That's because your syntax isn't quite right. Try this:

      Code:
      obj_files/AP_commons.o: AP_commons.cpp
         g++ AP_commons.cpp -g -O2 -Wall -MT obj_files/AP_commons.o -MD -MP -MF
      "./obj_files/AP_commons.Tpo" -c
      You can't specify where to compile to in the compile line itself, instead you tell it what output file the compile line belongs to.

      The syntax for compiling only goes:
      Code:
      Target: All dependancies (including libraries)
         compiler command

    7. #7
      Member
      Join Date
      May 2009
      Posts
      139
      Likes
      1
      Quote Originally Posted by ninja9578 View Post
      That's because your syntax isn't quite right. Try this:

      Code:
      obj_files/AP_commons.o: AP_commons.cpp
         g++ AP_commons.cpp -g -O2 -Wall -MT obj_files/AP_commons.o -MD -MP -MF
      "./obj_files/AP_commons.Tpo" -c
      You can't specify where to compile to in the compile line itself, instead you tell it what output file the compile line belongs to.

      The syntax for compiling only goes:
      Code:
      Target: All dependancies (including libraries)
         compiler command
      That one didn't work either g++ thought the *.0 and *.Tp0 were the input files and said it couldn't find them.

      I noticed that I couldn't find any place where it actually used the *.Tpo files, so I experimented this morning with a very simple command line of
      g++ AP_commons.cpp -c
      then moving the *.o files to obj_files manually. That cured most of the problems.

      But then I discovered that g++ couldn't find lroundf in the math library, even though it's there just as much as the other math functions the code uses. Fortunately it was simple to replace lroundf with the expression of (int)(n+0.5)

      After that, g++ couldn't find the system getopt.h, even though it's just as much there as any of the other system inlcudes. So I had to specify the absolute file location in the source code.

      But now, the g++ linking stage can't find getopt_long in getopt.h, even though it is there. And I have no idea how to fix that

    8. #8
      Explorer of the Mind SuperSmashcz's Avatar
      Join Date
      Jul 2008
      Gender
      Location
      MD
      Posts
      51
      Likes
      0
      Okay so I've started writing my own 3D engine in C++.. it's got a terrain engine and lighting.. ect. How do I take all my classes and turn it into a library? So like i could be like DDHI_MakeTerrain(..) rather than being like including the classes and calling MakeTerrain(..) Basically.. i could wrap up all my classes and import them all using a single .lib and use it throughout other programs i write using those classes?
      From every time we meet, to every time we part, i will add another memory to my shattered heart, and for every dream we chase, another memory will take its place, so we can remember it one day.

      LD's 15
      DILDs 14
      WILDS 1

    9. #9
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      It depends on your platform and compiler.

      First thing that you need to do is declare all of the methods that you want to use as static stdcall. You can't use classes directly, you need to write an interface. something like this:

      Code:
      //This is the interface header, compiled with both your library and your program
      #include "yourclass.h"
      
      static __stdcall void * New(void){
         return (void*)new yourclass();
      }
      
      static __stdcall SomeMethod(void * handle, int other_params...){
         if (handle){
            yourclass * pthis = (yourclass *)handle;
            pthis -> SomeMethod(other_params...);
         }
      }
      Then you need to set up your IDE to spit out a dll instead of an exe. It depends on your IDE on how to do that, if you're using a makefile, then you have to figure it out yourself, I don't deal with makefiles anymore

      You may or may not need to create and define a DEF file, all that does is tells the compiler what the names of the exported methods are.

      Most compilers will spit out both a dynamic and a static library, dynamic ones are probably better because you can change your engine on the fly. That's how games that use both OpenGL and DirectX do it.
      Last edited by ninja9578; 07-11-2009 at 12:17 AM.

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •