Using GCC through Xcode to compile basic programs - c++

So, I'm a brand new CS student, on a Mac, and I'm learning C++ for one of my classes. And I have a dumb question about how to compile my super basic C++ program.
I installed Xcode, and I'm looking through the documentation to try and figure out how to use it (and I highly suspect it's extremely overpowered for what I'm doing right now) and eventually end up going into Terminal and going "gcc [filename]". And I've got a screen full of text that starts with "Undefined Symbols", and goes on about trying to reference things, so I'm wondering if I didn't hook up something somewhere, especially as when I'm actually in Xcode with a C++ program open, most of the menu items are greyed out.
So. In really really basic terms. What did I miss doing, and how do I fix it? Is there a basic guide to Xcode? Most of the documentation is aimed at real developers, and I'm totally missing a lot of what is being assumed.

If XCode is installed then everything is set up correctly.
If you typed gcc on the command line then you invoked the 'C' compiler (not the C++ compiler). Usually this does not matter as GCC compensates by looking at the file extension. But what does matter is that it does not invoke the linker with the correct C++ flags.
What you should do (from the command line) is use g++
g++ <fileName>.cpp
By default the output file is a.out and placed in the same directory.
g++ has a flag to specify a different output name -o
g++ -o <outputName> <fileName>.cpp

Related

linker returns undefined reference to 'kernel_main()'?

I like programming, and recently I have been interested in operating systems. So once I found a working tutorial I would start programming, and modifying as much as I knew was possible (at least for me). But there came a time where I realized that programming everything in assembly just wasn't all that great, and I wanted to try programming an operating system in c++ and assembly. Every tutorial out there says it works, but I get one common error when linking: Undefined reference to 'kernel_main', even though the function exists. I tried to see if I spelled it wrong, but I didn't. I also made sure that everything was exactly as it was in the tutorials. Here is the link to an example that didn't work for me:
http://www.cplusplus.com/articles/zv07M4Gy/
I have indeed gotten past the compilation. The problem is simply sat within the linking of the two object files. Thanks in advance.
NOTE: I am running a 64 bit version of windows 10, so I am using the linker built in to gcc instead of the default windows one. Keep in mind that other tutorials also say to use C as the language for the kernel, and I am also doing that.
EDIT:
For one tutorial, I would use:
gcc_directory/i686-w64-mingw32/bin/ld.exe -m elf_i386 -T linker.ld -o main.bin boot.o kernel.o
For the one linked above, you can find the compilation info in the "How to compile it" section.
EDIT2: https://www.youtube.com/watch?v=wmR-_rxWvYc&t=234s

How does the GNU linker decide what C/C++ library files are needed?

I'm building PHP7 on an OpenWRT machine (an ARM router). I wanted to include MySQL, so I had to build that as well. OpenWRT is 99.5% ordinary linux, but there are some weird building / shared library things that probably don't get exercised often, so I've run into some difficulties.
MySQL builds OK (after some screwing around) and I have a libmysqlclient.so that works. However, the configure process for PHP7 fails when trying to link the MySQL test program, because libmysqlclient.so must be linked with the C++ standard libraries, not the C standard libs. (MySQL is apparently at least partially C++, and it uses std::...stuff....) Configure tries to compile the test program with gcc, which doesn't include the C++ libraries in the link, so the test fails.
I bodged over this by making a simple C/C++ switching script: if the command line includes -lmysqlclient then I exec g++ $* else exec gcc $*. Then I told configure to use my script as the C compiler.
It occurs to me that there must be a better way to handle this, though. It seems like libmysqlclient.so should have some way to tell the linker that it also needs libstdc++.so, so that even if gcc is used to link, all the necessary libraries would get pulled in.
Is there some way to mark dependencies in libmysqlclient.so? Or to make configure smarter about running test programs?
You should virtually never try to link with the C++ standard library manually. Use g++ for linking C++ programs. gcc knows the minute details of what library to use and where it lives, so you don't have to.
Now the question is, when to use g++, and when not to. One possible answer to that question is "always use g++". There is no harm in it. g++ can link C programs just fine. There is no overhead in the produced program. There might be some performance loss in the link process itself, but it probably won't be noticeable for any but the most humongous of programs.

I cannot compile this simple C++ program involving gd.h

This isn't my code, I am not a programmer but I did not expect simply compiling a provided source code would be so difficult.
Here it is, taken from Joel Yliluoma's page about "arbitrary-palette positional dithering algorithm", it was written in 2011.
This was my troubleshooting process, using MinGW:
The code didn't seem to make sense at all, so I realized it was written in an earlier version of C++, and added -std=c++98.
It couldn't find gd.h, I downloaded that from libgd's website, and directed to its directory using -I.
A bunch of gd related commands got a "undefined reference to" treatment. I tried to direct the compiler to gd.h/gd.c directory again using -l and followed by -lgd. And this is where I got stuck, as
The compiler insisted on not being able to find -lgd. I tried with different versions of libgd (especially older ones, before 2011) and sometimes it'd find what it's looking for, but then skip over them as they are incompatible.
I've also tried to compile it with another program called Dev-C++ but to no avail. Dev-C++ also gave back a "linker error". I can only assume that I messed up linking the header or library somehow, but I do not know what those terms mean frankly and just wanted a working program so I can get back to my imagery stuff. Maybe I downloaded the wrong gd.h, or I'm missing a required thing. Any help would be greatly appreciated.
Here's my current final MinGW input:
g++ -std=c++98 -Ipath\to\libgd code.cpp -Lpath\to\libgd -lgd -o executable.exe
I can assure you that path\to\libgd contains gd.h (and a bunch of other gd related stuff) and either one of these depending on which version of libgd I found: libgd.lib, libgd.dll.a, lidgb.def, libgd.rc, libgd.so.
I'm using Windows 7 64-bit.

G++ compiler giving "undefined reference" error to functions I have clearly defined

I have referenced several other questions on this site and others addressing this topic and nothing has helped my issue so far.
I have two classes and a main program written in c++. Total 5 files. Everything is written originally in Visual Studio 2013 and compiles and runs there.
All are in a single folder and I use this command to compile them:
g++ myprogram.cpp
I get errors "undefined reference" errors for the contructors and destructors of both classes. Is there something obvious that I am doing wrong? If not I will post my code. Thank you.
When you invoke g++ directly like this, it will do all the steps by default, including linking. Since you haven't listed your other source files, you really just want to do compilation (it would seem) and then link the results of compiling each individual source file. You can use the "-c" option to make it just compile (rather than compile and link). Or, if you do want to build all your source files and link them together, then you should list all of the source files in question. That is:
g++ *.cpp -o yourexecutable # To compile and link all the sources
Or:
g++ -c yourfile.cpp -o yourobjectfile.o # To compile a single source
But really you shouldn't invoke g++ directly at all; there are build systems that provide a layer of abstraction on top of GCC (and other tool chains) that would be a far better, more portable, and simpler approach to building your application. For example, Bazel or Gradle would be better ways to build your program from the commandline. Though not the best or most modern build system, even using Make (and relying on its implicit rules) would be better than direclty invoking the compiler.

Compilation QT Creator project using classic terminal and gcc/g++

I'm tired searching in internet.
I created a klient-serwer program, which shut down computer just by their IP's on specific port.I made whole thing in C, everything works perfectly, i'm compiling my project using gcc with -Wall option and I've got a clear results.
But I had to create GUI so i decided to use QT Creator IDE, I transfered code from C into C++ and i made GUI. Program works but I have to compile this using g++ with -Wall option.
Thing is, I cannot compile anything.
g++ my_project.cpp -o mypro -Wall
ERROR: fatal error: QMainWindow: directory don't exist (in my native language)
compilation terminated
(same with other source files)
Any ideas?
It's propably very easy, but believe me, I want to compile this and go to sleep. Cheers
Why don't you use qmake? First, you can edit your *.pro file and add any compiler flags you wish (see Mitch's comment). Then, if you execute qmake and then make, it will show you a sequence of compilation commands with proper flags, linker and include paths and so on. After that you can just reproduce that commands manually if your teacher wants so.
The error shows that the compiler is trying to open QMainWindow as if it was a directory (if the translation is correct) or at least failing to locate where the include files for Qt are. You probably need to provide some -I option to hint the compiler as to where the Qt headers are, and make sure that the #include directives are correct in your code.