Skip to content

Instantly share code, notes, and snippets.

@muaddib1971
Last active August 12, 2024 10:30
Show Gist options
  • Save muaddib1971/01d40a57cf62014fbf775279a3220066 to your computer and use it in GitHub Desktop.
Save muaddib1971/01d40a57cf62014fbf775279a3220066 to your computer and use it in GitHub Desktop.
Makefiles
#include "file1.h"
#include <iostream>
void f1(){
std::cout << "hello from file 1\n";
}
#ifndef FILE1
#define FILE1
void f1();
#endif
#include "file2.h"
#include <iostream>
void f2(){
std::cout << "hello from file 2\n";
}
#ifndef FILE2
#define FILE2
void f2();
#endif
#include "file3.h"
#include <iostream>
void f3(){
std::cout << "hello from file 3\n";
}
#ifndef FILE3
#define FILE3
void f3();
#endif
#include "file1.h"
#include "file2.h"
#include "file3.h"
#include <cstdlib>
#include <iostream>
int main(){
f1();
f2();
f3();
return EXIT_SUCCESS;
}
#targetname: prereqs
# command
all:files
files:file1.o file2.o file3.o main.o
g++ file1.o file2.o file3.o main.o -o files
file1.o:file1.cpp file1.h
g++ -Wall -Werror -std=c++14 -c file1.cpp
file2.o:file2.cpp file2.h
g++ -Wall -Werror -std=c++14 -c file2.cpp
file3.o:file3.cpp file3.h
g++ -Wall -Werror -std=c++14 -c file3.cpp
main.o:main.cpp file1.h file2.h file3.h
g++ -Wall -Werror -std=c++14 -c main.cpp
.PHONY:clean
clean:
rm -f file1.o file2.o file3.o
#targetname: prereqs
# command
CC=g++
all:files
OBJECTS = file1.o file2.o file3.o main.o
CXXFLAGS = -Wall -Werror -std=c++14
files:$(OBJECTS)
$(CC) $(OBJECTS) -o files
%.o:%.cpp *.h
$(CC) $(CXXFLAGS) -c $<
.PHONY:clean
clean:
rm -f $(OBJECTS) files
#include <iostream>
#include <cstdlib>
typedef void * operation (void*, void *);
void * intadd(void * a, void * b)
{
*(int*)a += *(int*)b;
return a;
}
void * intsub(void * a, void * b){
*(int*)a -= *(int*)b;
return a;
}
void * dbladd(void * a, void * b){
*(double*)a += *(double*)b;
return a;
}
void * dblsub(void * a, void * b) {
*(double*)a -= *(double *)b;
return a;
}
void * perform(void* a, void* b, operation op) {
return op(a,b);
}
int main(void){
int a,b;
a=3;
b=4;
std::cout << "3 + 4 is:";
perform(&a,&b,intadd);
std::cout << a << std::endl;
a=3;
b=4;
std::cout << "3 - 4 is:";
perform(&a,&b,intsub);
std::cout << a << std::endl;
double da, db;
da=3.3;
db=4.4;
std::cout << "3.3+4.4 is ";
perform(&da,&db,dbladd);
std::cout << da << std::endl;
da=3.3;
db=4.4;
std::cout << "3.3-4.4 is ";
perform(&da,&db,dblsub);
std::cout << da << std::endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment