Building glut with Makefile in mac OSX - macos

completely Makefile newbie here. I was asked by a professor to submit a Makefile along with the actual source code. I'm having a hard time getting the linker to find the Glut files.
My application has the following structure
My main.cpp has the following dependencies:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "renderer.cpp"
My renderer.cpp has the following dependencies:
#define GL_SILENCE_DEPRECATION
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include <vector>
#include "reader.cpp"
#include "../../../Common/Point.h"
My reader.cpp has the following dependencies:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "../headers/reader.h"
#include "../../../Common/Point.h"
And finally my Point.cpp has the following dependencies:
#include <iostream>
#include <sstream>
#include "Point.h"
I have zero experience with Makefile, and all i know was taken from a youtube video i watch to have a base of what to do. The Makefile goes as follows:
engine: main.o renderer.o reader.o Point.o
g++ main.o renderer.o reader.o Point.o -o engine
main.o: main.cpp
g++ -c main.cpp $ ./renderer.cpp $(-framework GLUT -framework OpenGL -framework Cocoa)
renderer.o: renderer.cpp
g++ -c renderer.cpp $(-framework GLUT -framework OpenGL -framework Cocoa) $ ./reader.cpp $ ../../../Common/Point.h
reader.o: reader.cpp
g++ -c reader.cpp $ ../headers/reader.h $ ../../../Common/Point.h
Point.o: ../../../Common/Point.cpp
g++ -c ../../../Common/Point.cpp $ ../../../Common/Point.h
clean:
rm *.o
rm *.xml
rm *.3d
When i try to run the make, i get the error "Undefined symbols for architecture x86_64:" followed by every glut/Opengl function i call.
I know the makefile is completely bad written but i would really appreciate some help.

Related

Use a static version of libc from an application that uses a dynamic shared library

I am trying to compile the following components:
appl - an application which links to a dynamic shared library (libwrapper.so)
libwrapper.so - A library used by appl. This library invokes functions from a libbackend archive.
libbackend.a - A static archive that uses libc functions.
I want to ensure that the libc functions invoked by libbackend always uses a fixed libc implementation. So I am also archiving the needed libc.a in libbackend, in my compilation environment.
I want to achieve that when libbackend invokes any libc function, the archived libc function from the compilation environment should be invoked, not the function from libc.so version in the target environment.
Can this be achieved? Also, can this be achieved without changing the way appl is being compiled; i.e., can be this achieved by changing makefile of only libwrapper and libbackend?
This is what I have tried so far, and does not work:
[dev-env]# ls
appl.c backend.c backend.h Makefile wrapper.c wrapper.h
[dev-env]# cat appl.c
#include <stdio.h>
#include "wrapper.h"
void main()
{
printf("in appl\n");
wrapper();
}
[dev-env]# cat wrapper.h
void wrapper();
[dev-env]# cat wrapper.c
#include <stdio.h>
#include "wrapper.h"
#include "backend.h"
void wrapper()
{
printf("In wrapper\n");
backend();
}
[dev-env]# cat backend.h
void backend();
[dev-env]# cat backend.c
#include <stdio.h>
#include <gnu/libc-version.h>
#include "backend.h"
void backend()
{
printf("in backend\n");
printf("GNU libc version: %s\n", gnu_get_libc_version());
}
[dev-env]# cat Makefile
LIBC=$(shell gcc --print-file-name=libc.a)
all: libbackend.a libwrapper.so appl
libbackend.a: backend.c backend.h
gcc -static -fPIC -c backend.c -o backend.o
ar rcs libbackend.a $(LIBC) backend.o
libwrapper.so: wrapper.c wrapper.h libbackend.a
gcc -c wrapper.c
gcc -shared -o libwrapper.so wrapper.o libbackend.a
appl: appl.c
gcc -o appl appl.c -L . -lwrapper
clean:
rm *.o *.a *.so appl
[dev-env]#
From compilation environment:
[dev-env]# make
gcc -static -fPIC -c backend.c -o backend.o
ar rcs libbackend.a /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/libc.a backend.o
gcc -c wrapper.c
gcc -shared -o libwrapper.so wrapper.o libbackend.a
gcc -o appl appl.c -L . -lwrapper
[dev-env]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
[dev-env]# ./appl
in appl
In wrapper
in backend
GNU libc version: 2.28
In target environment:
[target]# ./appl
in appl
In wrapper
in backend
GNU libc version: 2.30
If what I am intending works, on target I should have seen the version output as "2.28".
Can this be achieved?
No.
This will lead to totally unpredictable behavior on any system with a different version of libc.so.6 installed. It's not something that you should even attempt.
I want to ensure that the libc functions invoked by libbackend always uses a fixed libc implementation.
Why? What problem are you imagining this will solve?
It would not solve anything, but is guaranteed to introduce new problems.

I cannot use termios.h in cygwin64

I am making an application where I think I will need to use termios.h But I have windows 10. I installed cygwin64. I type in gcc test.c -o test.exe in the terminal. I still get fatal error: termios.h: No such file or directory #include <termios.h> Is there something I had to do during installation?
The code is just prints hello world but I included termios.h
#include <stdio.h>
#include <termios.h>
int main(){
printf("Hello World!");
return 0;
}
Install the missing development package. To find which is, use cygcheck
$ cygcheck -p usr/include/termios.h
Found 12 matches for usr/include/termios.h
cygwin-devel-3.0.7-1 - cygwin-devel: Core development files
...
cygwin-devel-3.2.0-0.1 - cygwin-devel: Core development files
cygwin-devel-3.2.0-1 - cygwin-devel: Core development files
...
You need cygwin-devel
$ cygcheck -l cygwin-devel |grep termios.h
/usr/include/termios.h
/usr/include/machine/termios.h
/usr/include/sys/termios.h
looking at your example
$ cat prova.c
#include <stdio.h>
#include <termios.h>
int main(){
printf("Hello World!");
return 0;
}
and at the compiler
$ which gcc
/usr/bin/gcc
$ gcc --version
gcc (GCC) 10.2.0
the example builds fine
$ gcc -Wall prova.c -o prova
$ ./prova
Hello World!
Instead of this:
#include <termios.h>
This:
#include <sys/termios.h>

How to use precompiled headers with gcc if linking against openmp

Minimal example:
// file: main.cpp
#include "pch.h"
int main()
{
std::cout << "test" << std::endl;
return 0;
}
--
// file: pch.h
#include <iostream>
Works fine and as expected if I compile this with
g++ pch.h
g++ main.cpp -Winvalid-pch
However once I change the last line to:
g++ main.cpp -fopenmp -Winvalid-pch
usage of the precompiled header is disabled:
warning: pch.h.gch: not used because `_REENTRANT' is defined [-Winvalid-pch]
How can I still use precompiled headers while linking to OpenMP? Why does the _REENTRANT define conflict with using a precompiled header at all?
You must generate .pch and compile sources with identical flags. -fopenmp implies #pragma omp and -pthread.
g++ -fopenmp pch.h
g++ main.cpp -fopenmp -Winvalid-pch
Or at least
g++ -pthread pch.h
g++ main.cpp -fopenmp -Winvalid-pch

Make - what files do I need to state as prerequisites for a target with multiple #include statements?

Assuming I have this example.h:
// example.h
#include "a.h"
#include "b.h"
#include "c.h"
#include "d.h"
#include "e.h"
And example.c:
// example.c
#include "example.h"
#include "a.h"
#include "b.h"
#include "c.h"
#include "d.h"
#include "e.h"
Should my Makefile look like this? :
example.o : example.h example.c
gcc -c example.c -o example.o
Or should it look like this? :
example.o : example.h example.c a.h b.h c.h d.h e.h
gcc -c example.c -o example.o
1) It should look like your second version:
example.o : example.h example.c a.h b.h c.h d.h e.h
gcc -c example.c -o example.o
2) There is no reason for the same #include statement to appear in both example.h and example.c, and redundant #include statements are kruft and ought to be removed. For safety, put them in example.h; for efficiency, put them in example.c when you can. (I won't go into the details of when you can here, but the compiler will tell you when you get it wrong.)
3) If you don't want to maintain that list of headers in the makefile, gcc and Make can take care of it for you. Here's an example, but this is an advanced technique, so I advise you not to use it until you understand how it works:
example.o : example.c
gcc -MMD -c example.c -o example.o
-include *.d

Makefile and CImg

I am having trouble developing a makefile for a code using the CImg library. I have 3 files:
mainProgram.cpp
program.cpp
program.h
CImg.h // CImg library
In the mainProgram.cpp
#include "program.h"
In the program.cpp
#include "program.h"
In the program.h
#ifndef PROGRAM_H
#define PROGRAM_H
#include "CImg.h"
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
using namespace cimg_library;
I am using a MAC and it suggested to compile it using: g++ -o snake mainSnake.cpp -O2 -lm -lpthread -I/usr/X11R6/include -L/usr/X11R6/lib -lm -lpthread -lX11
But, I am having difficulty communicating this to a makefile. Can anyone assist me?
The simplest make file would be
all:mainProgram.cpp program.cpp program.h
g++ -o snake mainSnake.cpp -O2 -lm -lpthread -I/usr/X11R6/include -L/usr/X11R6/lib -lm -lpthread -lX11
If you read some basics of makefile writing then you can first create the object files then create the final snake executable
May be this would help you get started with. The similar example is given but in c.

Resources