How to use libraries and headers in C++ with MinGW? - c++

I want to use OpenGL GLEW library. I have the binary downloaded and its folder is in the folder with my .cpp file. My .cpp file uses #include <eglew.h>.
How should I format my command for MinGW to compile my .cpp file? Do I compile with the .lib file like g++ -L./path/to/lib/file.lib test.cpp -o test or do I do something else like link to the header files g++ -I./path/to/headers test.cpp -o test?

To better understand things maybe it's better to split compiling and linking steps.
If you get errors then you will also know in which step the problem occurs.
I'm assuming you have the following folders/files:
/path/to/eglew/include/GL/eglew.h
/path/to/eglew/lib/libglew32.a
Compiling:
g++ -Wall -c -o test.o test.cpp -I/path/to/eglew/include/GL
Linking:
g++ -o test.exe test.o -L/path/to/eglew/lib -lglew32
Though I would expect to see #include <GL/eglew.h> in which case the linker include flag should be -I/path/to/eglew/include.

Related

how can I not create precompiled header when compile object file?

I use g++ 10.2.0 and try to create a static library, but when I create object file for archiving a static library, object file format always shows precompiled header, it makes the final static library cannot work:
//file static_test.cpp
void fn(){
int temp;
++temp;
}
//file static_test.h
void fn();
build them but not link
g++ -c static_test.h static_test.cpp -o static_test.o
use file to show static_test.o format
file static_test.o
static_test.o:GCC precompiled header (version 014) for C++
and I archive it
ar rsv libstatic_test.a static_test.o
use file to show libstatic_test.a format:
current ar archive
use a main.cpp to test this static library
#include "static_test.h"
int main(){
fn();
return 0;
}
compile them and link
g++ main.cpp libstatic_test.a
libstatic_test.a: cannot add symbol: archive has no index;run ranlib to add one
collect2: error:ld return 1
why and how to solve this problem, tks~
-c is only for a single file, the second static_test.cpp is ignored. You should get the compiler warning about multiple files set to -c. g++ -c static_test.h results the precompiled header in static_test.o and static_test.cpp is ignored. The proper command should be
g++ -c static_test.cpp -o static_test.o
Do not pass header files to the compiler when you compile object files. All other commands you are using look ok.
if you would like to create a static library with gcc, you have to say it to the linker/wrapper programm "gcc" like:
gcc -static -o libyourlibname(.lib/.so) obj1.o obj2.o -s
legende:
-static: tells the linker to build a static lib
-o : output file
-s : strip all debug/linking stuff, including debug informations
note:
may be you need the option -fPIC at .c compile time like:
gcc -O2 -fPIC -c file1.c -o file1.o
legende:
-O2 : tells the c compiler to optimize
-fPIC : create program independet code (internal for the output code)
-c : compile C file to object file:
-o : tell the linker how the object file should be named
By the way:
Pre-compiled header files are only created by compiling C/C++ files only.
You have require huge memory, and mostly pre-compiled header files are not needed in small projects of small student homework tasks.
And each time you change the header file, you (the compiler) have to create a new copy of the .pch file.
Of course, .pch files are good for end-products which does not change it in the form for the developer. But they are mostly depend on the compiler.
So, you can't use .pch files from Windows MinGW64 Project under Linux (with the near) same compiler in different versions.

Compiling C files along with C++ file in g++

I'm having a custom C header file that I have created. There are several files in my directory as follows.
lib1/
-lib1.h
-lib1.c
lib2/
-lib2.h
-lib2.c
-lib_main.c
-lib_main.h
-main.c
-main.cpp
-Makefile
Now, for testing the header file with a test file called main.c, I will be giving the following command in the terminal,
gcc lib_main.c lib1/lib1.c lib2/lib2.c main.c -o ./main
Now, how could I test the same header files with main.cpp instead of main.c, how do I change it?
You should (and most probably must) compile separately the c and c++ sources into a object file, and then link together.
As an example
gcc -c -o lib1.o lib1/lib1.c
gcc -c -o lib2.o lib2/lib1.c
gcc -c -o lib_main.o lib_main.c
g++ -c -o main.o main.cpp
g++ -o main lib1.o lib2.o lib_main.o main.o
The first four commands create a series of object files, with extension .o. The last command link together the object files into an executable.
Obviously you need to add the relevant compiler options for each source.
Two important points to notice:
Order of files in the linking is important. See discussion here and here.
To mix c and c++ code with each other, for example if a c++ code calls a c function, you need to follow specific guidelines.

Geany, g++ and SDL errors in compilation

So, I was following a simple C++ with SDL tutorial for linux but i encounter some errors on my way.
First of all I'm using Geany and i downloaded the corresponding SDL2 libs, here is the thing:
in my project folder there is a main.cxx file, which i open with geany as i mentioned before:
I included this libraries:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
First i encountered a pelculiar error, compilation performs sucessfully but when it comes to build i got this error:
main.cxx: undefined reference to `SDL_Init'
After searching a bit i found out that i had to add the tag -lSDL to my geany build options so they would end up being somethinf like this:
Compile:
g++ -Wall -c -lSDL "%f"
Build:
g++ -Wall -o -lSDL "%e" "%f"
But there is a problem, now when I execute the build command i get a:
G ++: error: main: There is no such file or directory
Why am i getting this error, am I including a wrong library or g++ has problems with .cxx files?
I already tried converting between .cxx and .cpp.
Thanks in advance.
g++ -Wall -c -lSDL2 "%f"
There is absolutely no need to specify libraries during compilation phase. Remove -lSDL.
g++ -Wall -o -lSDL2 "%e" "%f"
It invokes compiler, implies linking (no -c or other operation-specific flags), and sets output file name to -lSDL2. That is, linker will output resulting binary in a file named -lSDL2 in current working directory. Then, when it comes what files to link, it goes main, which supposed to be -o main, but since you've broken flags order it is now just ordinary file name that linker will try to link into resulting binary. It so happens that this file doesn't exist.
Long story short, make correct linking line - g++ -o "%e" %f -lSDL2 (libraries comes last, library order is also important).

How do I link multiple files without the use of a makefile?

For example, I'm given carModels.cpp, carModels.h, carType.in, manufacturers.h, manufacturers.o, and lastly my own file tester.cpp. How would I go about linking all of these using g++ in a Linux terminal? Would I have to create any additional ".o" files? I'm supposed to assume that the given files already work. Multiple lines in terminal are fine, I just I want a clear understanding of it. (I'm coming from a C++ IDE that I didn't really care for.)
Compile each source file to its own object file:
g++ -I . -c carModels.cpp -o carModels.o
g++ -I . -c tester.cpp -o tester.o
Now link all object files together:
g++ carModels.o tester.o manufacturers.o -o outputname
Consider adding more options like -O3, -std=c++11, -Wall, etc. as needed.
you can do this in two steps, first compile to *.o files,
gcc -c your.cpp other.cpp .....
then link them
gcc -o you_out_put_name the_object_files.o ...
In a single line, that would be just g++ -o tester *.cpp *.o. GCC will sort everything out. In particular, the *.h files are referenced via #include "" statements in the .cpp files.

C++ programs, compiling with g++

I am very aware of compiling C++ programs with g++ in linux environment. But, may be I am missing something, I am getting this strange output/behaviour.
I have source file in test.cpp.
To compile this, I did
(1)
g++ -c test.cpp
g++ -o test test.o
./test
Everything works fine.
But when I did compling and linking in same stage, like this
(2)
g++ test.cpp -o test
./test => Works fine
(3)
g++ -c test.cpp -o test => Doesn't work
In my last case, test is generated but is no more executable; but in my guess it should work fine.
So, what is wrong or do I need to change some settings/configuration ??
I am using g++ 4.3.3
Thanks.
When you say:
g++ -c test.cpp -o test
The -c flag inhibits linking, so no executable is produced - you are renaming the .o file.
Basically, don't do that.
You are forcing compiler to produce an object file and name it like an executable.
Essentially your last line tells: compile this to an object file, but name it test, instead of test.obj.
-c flag means Compile Only
Try
g++ -o test test.cpp
Specifying -o in the g++ command line tells the compiler what name to give the output file. When you tried to do it all in one line, you just told the compiler to compile test.cpp as an object file named test, and no linking was done.
Have a look at the fabulous online manual for GCC for more details.
from the gcc manual:
-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an
object file for each source file.
You must link the compiled object files to get the executable file.
More info about compiling and linking and stuff is here.
Read man g++. The switch -c is to compile only but not to link.
g++ -c test.cpp -o test
does what
g++ -c test.cpp
does but the object file will be test istead of the default name test.o. An object file cannot be executed.