Like Tree5Likes
  • 3 Post By brewbuck
  • 2 Post By Elysia

Ubuntu g++ compiler - Beginner commands

This is a discussion on Ubuntu g++ compiler - Beginner commands within the C++ Programming forums, part of the General Programming Boards category; So after much reading I was coaxed into trying a Unix system and getting out of the windows environment I ...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407

    Ubuntu g++ compiler - Beginner commands

    So after much reading I was coaxed into trying a Unix system and getting out of the windows environment I was coding in.

    It seriously took me all afternoon just to figure out how to compile a program that had a personally written .h file. Now that I have finally figured out how to compile it with the propper commands. I was just wondering if anyone had any suggestions or advice for coding in this new environment.

    This isn't really a question just a place that anyone who has used this environment can drop tips or experience they've learned over the years.

    What I know so far
    Code:
    g++ sort.cpp -c -I/home/user/directorytoinclude -Wall -W -Werror // will compile to a .o
    g++ sort.o main.cpp -I/home/user/directorytoinclude -Wall -W -Werror  // will compile using the .o
    You'll probably laugh but it seriously took me all afternoon to learn the correct syntax in this. I didn't see alot of information out there directed at noobies coding in the environment.

    Thanks!

  2. #2
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Looks way too complicated.

    I have been using CodeBlocks for a while, so I don't remember exactly about the path-option for your header, but I'd do it something like this:

    Code:
    g++ -Wall -pedantic -I/home/... sort.cpp main.cpp -o myprogram
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    or even something like this
    Code:
    g++ -Wall -pedantic -I/home/... sort.cpp -o sort.o
    g++ -Wall -pedantic -I/home/... main.cpp -o main.o
    g++ -o myprogram main.o sort.o
    a makefile makes (pun intended ) this a lot easier.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Makefiles are the way to go in UNIX-like environments. Everybody has their own preferred makefile template. If you want one to start with, you could try this:

    Code:
    CXX				= g++
    
    # List any extra include paths you need on the following line, in the form -I<path>
    INCLUDE			=
    
    # List any extra macro definitions you need on the following line, in the form -D<macro>
    DEFINE			=
    
    # Comment the following line to disable debug symbols
    DEBUGFLAGS		= -g
    
    # Uncomment the following line to enable optimization
    #OFLAGS			= -O3
    
    # Uncomment the following line to enable gprof profiling
    #PROFFLAGS		= -pg
    
    CXXFLAGS		= $(INCLUDE) $(DEFINE) $(DEBUGFLAGS) $(OFLAGS) $(PROFFLAGS)
    
    # List any libraries you want to link against on the following line, in the form -L<path> or -l<lib>
    LIBS			=
    
    LDFLAGS			= $(LIBS) $(PROFFLAGS)
    
    # List all your .cpp files on the following line
    SRCS			= a.cpp b.cpp c.cpp
    
    # List all your .h files on the following line
    HDRS			= a.h b.h c.h
    
    OBJS			= $(SRCS:.cpp=.o)
    
    # Choose the name of the program file
    TARGET			= MyProgram
    
    .PHONY: all clean
    
    all: $(TARGET)
    
    $(TARGET): $(OBJS)
    	$(CXX) -o $@ $^ $(LDFLAGS)
    
    clean:
    	rm -f $(TARGET)
    	rm -f $(OBJS)
    	
    .dep: $(SRCS) $(HDRS)
    	$(CXX) -M $(CXXFLAGS) $(SRCS) > .dep
    	
    -include .dep
    Place the above into a file called "Makefile" (with capital 'M'), tweak as indicated, then run "make".

    Note that I typed that from memory, so it may contain bugs, though I did pay attention while writing it
    Last edited by brewbuck; 12-12-2012 at 09:01 PM. Reason: Was missing the OFLAGS
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Oh wow I never even heard of this. Will def google a tutorial when I get home and play around with the one you have provided. Thanks brewbuck. Also thanks to Elkvis and Dennis.cpp for the -pedantic. I had not heard of that one and the cmd 'man g++' got me some new info I will have to google as well (strict ISO I think it was).

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Don't forget -std=c++11 if it's not on by default (can't remember).
    dennis.cpp and Elkvis like this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Quote Originally Posted by Elysia View Post
    if it's not on by default (can't remember).
    Not yet.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sorry, this post is kind of disorganized. Random comments:
    • Running the resulting program normally requires you to specify the current directory, e.g. "./program". I remember this was the one point which took me an embarrassingly long time to figure out.
    • -I can take relative paths (i.e. you can say -Ietc or -I../etc) and optionally a space (-I ../etc).
    • The (arguably) canonical order for specifying a g++ command is
      Code:
      g++ language-flags includes source-files linker-flags
      e.g.
      Code:
      g++ -W -Wall -I../other -c foo.cpp
      g++ foo.o -o foo -lm
      where "-lm" links with the math library (if you use std::sin/std::cos or something).
    • You don't have to compile a c++ file to an object file first. g++ will create an object file automatically for you if you just say "g++ foo.cpp -o foo". This is fine for smaller projects but for big ones it's very slow, and in that case you (or your Makefile) would be compiling to object files first.
    • A large portion of all build systems are based on make, or generate Makefiles, so it's helpful to have some familiarity with make.


    For comparison, here is my Makefile template. I really hope there are no typos, but maker beware.
    Code:
    # Makefile for some project
    
    CXX = g++
    CXXFLAGS = -Wall -Wextra
    LDFLAGS = -lm
    
    TARGET = program
    SOURCES = $(wildcard *.cpp)
    OBJECTS = $(SOURCES:.cpp=.o)
    
    # Default target: all
    .PHONY: all
    all: $(TARGET)
    
    # Executable targets
    $(TARGET): $(OBJECTS)
    	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
    
    # Dependencies
    include depend
    depend: $(SOURCES)
    	$(CXX) $(CXXFLAGS) -MM $(SOURCES) > depend
    
    # Other targets
    .PHONY: clean
    clean:
    	-$(RM) $(TARGET) $(OBJECTS)
    It's quite similar to brewbuck's with a few minor changes: it automatically grabs all the .cpp sources in the current directory and compiles them (you can list each one if you prefer). I use -MM rather than -M for dependencies to ignore system headers. $^ is the list of all dependencies and $@ is the name of the target file. Anyway, just another example to ponder. I personally learned make by running "info make" and reading most of it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    dwks, you should include all of your header files as dependencies of your "depend" rule. Otherwise, if you make a change to a header file that alters the dependency graph, it will not be noticed and the deps won't rebuild. In turn that can lead to actual source files not being rebuilt even if they should be.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    True enough, good point. To be honest I don't usually have a depends rule for small projects, easier just to make clean when I know I've changed a data structure... and for larger projects I'll use cmake or qmake or a similar tool. So it's been a while since I actually used gcc -MM in a Makefile. This might explain why I didn't do it properly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C++ Beginner] Which compiler i should use??
    By fredsilvester93 in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2012, 09:11 AM
  2. Beginner question.. wich compiler
    By viper2k in forum C Programming
    Replies: 7
    Last Post: 03-27-2011, 09:19 AM
  3. Beginner question: Error with the compiler
    By d3lilley in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2010, 05:05 PM
  4. Replies: 2
    Last Post: 02-04-2008, 01:34 AM
  5. ASM beginner gets 24 compiler errors on hello world...
    By Desolation in forum Tech Board
    Replies: 12
    Last Post: 06-16-2007, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21