Commit 5c002030 authored by gonczor's avatar gonczor

forgotten files

parent 8147a6d4
CXX := clang++
CXX_FLAGS := -Wall -Wextra -std=c++17 -g
BIN := bin
SRC := src
INCLUDE := include
LIB := lib
LIBRARIES :=
EXECUTABLE := main
all: $(BIN)/$(EXECUTABLE)
run: clean all
./$(BIN)/$(EXECUTABLE)
$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ $(LIBRARIES)
clean:
-rm $(BIN)/*
\ No newline at end of file
namespace mymath {
int add(int, int);
int sub(int, int);
}
#include <iostream>
#include <cstring>
#include <dlfcn.h>
int main(int argc, char** argv) {
const char* libName = "bin/mymath.A.dylib";
void *libHandle;
std::cout << "# main() starts" << std::endl;
libHandle = dlopen(libName, RTLD_NOW);
if(libHandle == NULL) {
std::cerr << "Error opening mymath:\n" << dlerror() << std::endl;;
return 1;
}
int (*mymath_add)(int, int) = (int(*)(int, int))dlsym(libHandle, "add");
if(mymath_add == NULL) {
std::cerr << "Error opening while getting address of add:\n" << dlerror() << std::endl;;
return 2;
}
int (*mymath_sub)(int, int) = (int(*)(int, int))dlsym(libHandle, "sub");
if(mymath_sub == NULL) {
std::cerr << "Error opening while getting address of add:\n" << dlerror() << std::endl;;
return 2;
}
std::cout << "# testing add: 55+66=" << mymath_add(55, 66) << std::endl;
std::cout << "# testing subtract: 55-66=" << mymath_sub(55, 66) << std::endl;
if(dlclose(libHandle) != 0) {
std::cerr << "Error closing library:\n" << dlerror() << std::endl;;
return 2;
}
std::cout << "# main() exits" << std::endl;
if(argc == 2 && strncmp(argv[1], "crash\n", strlen("crash\n"))){
std::cout << "# testing subtract: 55-66=" << mymath_sub(55, 66) << std::endl;
}
return 0;
}
all:
llvm-g++ -Iinclude -dynamiclib -std=c++17 src/mymath.cpp -current_version 1.0 -compatibility_version 1.0 -fvisibility=hidden -o bin/mymath.dylib
nonamespace:
llvm-g++ -Iinclude -dynamiclib -std=c++17 src/mymath_a.cpp -current_version 1.0 -compatibility_version 1.0 -fvisibility=hidden -o bin/mymath.A.dylib
#include <iostream>
#define EXPORT __attribute__((visibility("default")))
__attribute__((constructor))
static void _constructor() {
std::cout << "Initializing mymath\n";
}
EXPORT
extern "C" int add(int a, int b) {
return a + b;
}
EXPORT
extern "C" int sub(int a, int b) {
return a - b;
}
__attribute__((destructor))
static void _destructor() {
std::cout << "Destroying mymath\n";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment