Author Topic: How to setup for G++  (Read 7781 times)

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
How to setup for G++
« on: March 16, 2021, 01:03:59 am »
There are many compilers in the list, including GCC;  but G++ I cannot see;  and I know I have it installed...

EDIT:  Also, could someone recommend a link where I can read how to make a makefile?  I tried to make
one from an example I found in Stack Overflow, but now the compiler says there are no instructions for
how to build a Debug target.  I want life to be easy...

EDIT2:  A set of official makefile examples would be nice;  in fact, why can't the setup write a basic makefile that one can then modify as needed?  The example I found in Stack Overflow has gazillions of arguments I have no idea what they are for, namely, this is what it is with my renaming of files from the example:

Code
DL3D: DL3D.o math/numrepr.o
g++ -g -o DL3D DL3D.o math/numrepr.o -L/sw/lib/root -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint \
-lPostscript -lMatrix -lPhysics -lMathCore -lThread -lz -L/sw/lib -lfreetype -lz -Wl,-framework,CoreServices \
-Wl,-framework,ApplicationServices -pthread -Wl,-rpath,/sw/lib/root -lm -ldl

DL3D.o: DL3D.cpp math/numrepr.h
g++ -g  -c -pthread -I/sw/include/root DL3D.cc

math/numrepr.o: math/numrepr.h math/numrepr.cpp
g++ -g -c -pthread -I/sw/include/root math/numrepr.cpp

but I have no idea what those libraries, if they are libraries, are, or weather I need them.
I understand that Code Blocks is just an environment;  but it would be a nice little extra...
Or, could someone recommend a GUI tool I could use to generate a makefile?

UPDATE:  Found cbp2make and used it to get an initial makefile.
I'm now in the process of learning to understand the format and customizing it.
The main problem is my project was not properly set up;  I had a math cpp file that my main.cpp needed, but
creating the file in CB doesn't add it to the project necessarily;  so cbp2make read the project file and re-created
the problem I had.
Another bug in cbp2make is it names the targets in lowercase;  CB expects them capitalized.  Another problem
is it assumes the main file is named "main.cpp", which for me wasn't true;  I had renamed it to DL3D.cpp.
 So, but I managed to add the math file to the make.

This is what my makefile looks like so far:

Code
#------------------------------------------------------------------------------#
# This makefile was generated by 'cbp2make' tool rev.147                       #
#------------------------------------------------------------------------------#

WORKDIR = `pwd`

CC = g++
CXX = g++
AR = ar
LD = g++
WINDRES = windres

INC =
CFLAGS = -Wall
RESINC =
LIBDIR =
LIB = -lGL -lX11
LDFLAGS = -L/sw/lib/root

INC_DEBUG = $(INC)
CFLAGS_DEBUG = $(CFLAGS) -g
RESINC_DEBUG = $(RESINC)
RCFLAGS_DEBUG = $(RCFLAGS)
LIBDIR_DEBUG = $(LIBDIR)
LIB_DEBUG = $(LIB)
LDFLAGS_DEBUG = $(LDFLAGS)
OBJDIR_DEBUG = obj/Debug
DEP_DEBUG =
OUT_DEBUG = bin/Debug/DL3D

INC_RELEASE = $(INC)
CFLAGS_RELEASE = $(CFLAGS) -O2
RESINC_RELEASE = $(RESINC)
RCFLAGS_RELEASE = $(RCFLAGS)
LIBDIR_RELEASE = $(LIBDIR)
LIB_RELEASE = $(LIB)
LDFLAGS_RELEASE = $(LDFLAGS) -s
OBJDIR_RELEASE = obj/Release
DEP_RELEASE =
OUT_RELEASE = bin/Release/DL3D

OBJ_DEBUG = $(OBJDIR_DEBUG)/DL3D.o $(OBJDIR_DEBUG)/num_repr.o

OBJ_RELEASE = $(OBJDIR_RELEASE)/DL3D.o $(OBJDIR_RELEASE)/num_repr.o

all: debug release

clean: clean_debug clean_release

before_debug:
test -d bin/Debug || mkdir -p bin/Debug
test -d $(OBJDIR_DEBUG) || mkdir -p $(OBJDIR_DEBUG)

after_debug:

Debug: before_debug out_debug after_debug

out_debug: before_debug $(OBJ_DEBUG) $(DEP_DEBUG)
$(LD) $(LIBDIR_DEBUG) -o $(OUT_DEBUG) $(OBJ_DEBUG)  $(LDFLAGS_DEBUG) $(LIB_DEBUG)

$(OBJDIR_DEBUG)/DL3D.o: DL3D.cpp
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c DL3D.cpp -o $(OBJDIR_DEBUG)/DL3D.o

$(OBJDIR_DEBUG)/num_repr.o: math/num_repr.cpp
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c math/num_repr.cpp -o $(OBJDIR_DEBUG)/num_repr.o

clean_debug:
rm -f $(OBJ_DEBUG) $(OUT_DEBUG)
rm -rf bin/Debug
rm -rf $(OBJDIR_DEBUG)

before_release:
test -d bin/Release || mkdir -p bin/Release
test -d $(OBJDIR_RELEASE) || mkdir -p $(OBJDIR_RELEASE)

after_release:

Release: before_release out_release after_release

out_release: before_release $(OBJ_RELEASE) $(DEP_RELEASE)
$(LD) $(LIBDIR_RELEASE) -o $(OUT_RELEASE) $(OBJ_RELEASE)  $(LDFLAGS_RELEASE) $(LIB_RELEASE)

$(OBJDIR_RELEASE)/DL3D.o: DL3D.cpp
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c DL3D.cpp -o $(OBJDIR_RELEASE)/DL3D.o

$(OBJDIR_RELEASE)/num_repr.o: math/num_repr.cpp
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c math/num_repr.cpp -o $(OBJDIR_RELEASE)/num_repr.o

clean_release:
rm -f $(OBJ_RELEASE) $(OUT_RELEASE)
rm -rf bin/Release
rm -rf $(OBJDIR_RELEASE)

.PHONY: before_debug after_debug clean_debug before_release after_release clean_release

It works as far as compiling the two files;  but the linker tells me that all the functions from the math file
are undefined...   :(

By the way, my first question still stands;  why is there not G++ in the list of compilers to choose from?
I even changed the first definition in the make file from "CC = gcc" to "CC = g++", to try and force it to
use G++, but the compiler output still announces itself as GCC...  ::)
« Last Edit: March 16, 2021, 03:51:55 am by DanW_58 »

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #1 on: March 16, 2021, 04:14:38 pm »
Could someone PLEASE help?!
Why does G++ not show in the list of compilers?
I also want to understand the purpose and relation between the cbp file and the makefile:  I used cbp2make yesterday, but if the cbp is not right it makes a makefile that is not right;  so how do I fix the cbp file in the first place?  How do I tell it what files to compile and link?  I edited the cbp manually to add my other cpp file;  but it is not linking them together.  Is this a programming question?  I don't think it is.
« Last Edit: March 16, 2021, 04:27:08 pm by DanW_58 »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: How to setup for G++
« Reply #2 on: March 16, 2021, 04:34:35 pm »
G++ is part of GCC. C::B will use GCC for .c files and G++ for .cpp files (see Toolchain executables in Settings -> Compiler).

If you want to add files to the CBP project just right-click on the name of the project and select "Add files...".

You don't need makefiles when working with C::B, but if you need to use one then you can check "This is a custom Makefile" in Project/target options -> Project settings.

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #3 on: March 16, 2021, 04:46:02 pm »
Ah, okay;  many thanks;  I was having a compile problem that I found an answer for saying "I had the same problem with GCC;  just switch to G++", so I thought they were separate compilers.
I have the "This is a custom makefile" check on.
I will try removing the makefile and using "add files", though I have a feeling I already tried that before.

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #4 on: March 16, 2021, 04:59:24 pm »
Okay, I removed the makefile, and I added my two .cpp files with right-click Add Files...;  but when I try to build project I get "No input files".

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #6 on: March 16, 2021, 05:35:02 pm »
Yeah, I wouldn't know how to build from the command line;  I used to do my programming in Windows, using gui's;  I'm new to linux and the command line world.
The build log:
Code
-------------- Build: Debug in DL3D (compiler: GNU GCC Compiler)---------------

Skipping file (no compiler program set): math/num_repr.cpp
gcc  -o bin/Debug/DL3D   
gcc: fatal error: no input files
compilation terminated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

This is how my two files appear in the cbp file:
Code
		<Unit filename="DL3D.cpp">
<Option compilerVar="CXX" />
</Unit>
<Unit filename="math/num_repr.cpp">
<Option compilerVar="CXX" />
</Unit>
Is that correct?

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #7 on: March 16, 2021, 05:39:39 pm »
Actually, I replaced "CXX" with "g++" but doesn't seem to help much...

Code
-------------- Clean: Debug in DL3D (compiler: GNU GCC Compiler)---------------

Cleaned "DL3D - Debug"

-------------- Build: Debug in DL3D (compiler: GNU GCC Compiler)---------------

Skipping file (no compiler program set): DL3D.cpp
Skipping file (no compiler program set): math/num_repr.cpp
gcc  -o bin/Debug/DL3D   
gcc: fatal error: no input files
compilation terminated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 

Offline gd_on

  • Lives here!
  • ****
  • Posts: 796
Re: How to setup for G++
« Reply #8 on: March 16, 2021, 05:51:58 pm »
The first thing you should do, is to find a tutorial on codeblocks and try to understand how it works.
You can also have a look on codeblocks site in the "User Manual", how to set a hello program project.
Windows 11 64 bits (23H2), svn C::B (last version or almost!), wxWidgets 3.2.4 (tests with 3.3), Msys2 Compilers 13.2.0, 64 bits (seh, posix : gcc, g++ and gfortran in C:\msys64\mingw64) or 32 bits (dwarf2, posix  in C:\msys64\mingw32).

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: How to setup for G++
« Reply #9 on: March 16, 2021, 05:54:28 pm »
Editing the CBP file is not a recommended practice, of course being XML does not help to prevent this.
You don't need the <Option compiler... stuff, just

Code
<Unit filename="DL3D.cpp" />
<Unit filename="math/num_repr.cpp" />

I would delete the project, recreate it using thw wizard, add the two files and forget about it being a text file.

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #10 on: March 16, 2021, 07:21:24 pm »
Many thanks, gentlemen;  I will do so.

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 81
    • Suryavarman
Re: How to setup for G++
« Reply #11 on: March 16, 2021, 07:39:09 pm »
From CB you can create a new empty project and compare it with your project.

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #12 on: March 16, 2021, 07:48:18 pm »
I just did;  OpenGL project wizard;  and this time I did not rename my main file;  played it safe.
But I was getting a most mysterious compiler problem, telling me that 'class' was an unknown type..
Just realized the main file that was created is "main.c"...  :(
Why?!!!
And now if I rename it to CPP it's going to break everything again?

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #13 on: March 16, 2021, 08:07:23 pm »
Never mind;  I deleted the files created, ran the wizard again, thinking it might have a hidden button to select language;  there was no such thing...
So I removed file main.c from the project, and added DL3D.cpp, as well as the other file, num_repr.cpp, and it compiles.
What I get now is,
Code
/usr/bin/ld: obj/Debug/DL3D.o: in function `main':
/home/dd/DL3D/DL3D.cpp:243: undefined reference to `num_repr<unsigned char>::init(int, int, int, int, int, int, int, int)'
/usr/bin/ld: /home/dd/DL3D/DL3D.cpp:245: undefined reference to `num_repr<unsigned char>::num_totalbits()'
/usr/bin/ld: /home/dd/DL3D/DL3D.cpp:241: undefined reference to `num_repr<unsigned char>::~num_repr()'
/usr/bin/ld: /home/dd/DL3D/DL3D.cpp:241: undefined reference to `num_repr<unsigned char>::~num_repr()'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 2 second(s))
5 error(s), 5 warning(s) (0 minute(s), 2 second(s))
 
... all of which functions ARE well defined in num_repr.cpp and declared in the included header num_repr.h;  so I think there's still a problem with linker setup.
Maybe it thinks it is linking C instead of C++?

EDIT:  Both files are actually compiled;  I see DL3D.o and num_repr.o in the obj folder;  but somehow it says 'undefined'...
« Last Edit: March 16, 2021, 08:14:02 pm by DanW_58 »

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 81
    • Suryavarman
Re: How to setup for G++
« Reply #14 on: March 16, 2021, 09:59:40 pm »
Do you have write the template core functions ? 

Code
typename<T>
num_totalbits<T>::num_totalbits()
{
}

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline DanW_58

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: How to setup for G++
« Reply #16 on: March 28, 2021, 04:11:37 am »
That's an amazing link;  a lot of my problems went away by bringing definitions of template class functions back to the header file.
Including seemingly unrelated linker problems;  I thought my .o file was not being seen, but after bringing back functions to the
header file it all compiles and links.  I can't say I understand why;  though;  I know nothing about cookies and ovens;  I don't bake.