Skip to content

Commit fafbeef

Browse files
committed
Add test for dlopen
1 parent 27b1a4f commit fafbeef

4 files changed

Lines changed: 48 additions & 6 deletions

File tree

lib/libprint.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
// To avoid renaming the function in the exported symbols
5+
extern "C" {
6+
void myprint();
7+
}
8+
9+
void myprint() { printf("It's working!\n"); }

lib/print.c

Lines changed: 0 additions & 4 deletions
This file was deleted.

makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ ifdef OS
2828
# Windows
2929
EXECUTE_FILE_TARGET := ${BINDIR}/executefile.exe
3030
TESTS_TARGET := ${BINDIR}/tests.exe
31+
LIB_TARGET := ${BINDIR}/libprint.dll
3132
else
3233
# Unix
3334
EXECUTE_FILE_TARGET := ${BINDIR}/executefile
3435
TESTS_TARGET := ${BINDIR}/tests
36+
LIB_TARGET := ${BINDIR}/libprint.so
3537
endif
3638

3739
EXES := ${EXECUTE_FILE_TARGET} ${TESTS_TARGET}
3840

39-
Build: BuildExecuteFile BuildTests
41+
Build: BuildExecuteFile BuildTests Lib
4042

4143
BuildExecuteFile: $(OBJS) $(MAINDIR)/ExecuteFile.o | bin
4244
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o ${EXECUTE_FILE_TARGET} $^
@@ -64,3 +66,5 @@ else
6466
$(foreach file, $(EXES), rm ${file};)
6567
endif
6668

69+
Lib: lib/libprint.cpp | bin
70+
$(CXX) $(CXXFLAGS) -shared -fPIC $(CPPFLAGS) -o ${LIB_TARGET} $<

src/mains/Tests.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,34 @@ void testFile(std::string path){
120120
std::cout << "[ OK ] " << path << std::endl;
121121
}
122122

123-
int main() {
123+
// TODO
124+
#include <dlfcn.h>
125+
126+
void testLibrary(){
127+
std::cout << "Testing library..." << std::endl;
128+
129+
void* handle = dlopen("bin/libprint.so", RTLD_LAZY);
130+
if (!handle) {
131+
std::cerr << "Error: " << dlerror() << std::endl;
132+
throwConstraintViolated("Failed to load library");
133+
}
134+
135+
dlerror(); // Clear any existing error
136+
137+
void (*myprint)() = (void (*)())dlsym(handle, "myprint");
138+
const char* dlsym_error = dlerror();
139+
if (dlsym_error) {
140+
std::cerr << "Error: " << dlsym_error << std::endl;
141+
dlclose(handle);
142+
throwConstraintViolated("Failed to find symbol in library");
143+
}
144+
myprint(); // Call the function from the library
145+
dlclose(handle); // Close the library handle
146+
147+
std::cout << "[ OK ] Library test passed." << std::endl;
148+
}
149+
150+
void suiteTestfiles(){
124151
testFile("mfiles/addition_infix.m");
125152
testFile("mfiles/addition.m");
126153
testFile("mfiles/broken_syntax.m");
@@ -145,7 +172,11 @@ int main() {
145172
testFile("mfiles/type_annotation.m");
146173
testFile("mfiles/struct.m");
147174
testFile("mfiles/struct_nested.m");
175+
}
148176

177+
int main() {
178+
suiteTestfiles();
179+
testLibrary();
149180
// Structs:
150181
// member functions for structs
151182

@@ -154,6 +185,8 @@ int main() {
154185
// blob type (alloc8, size, get(blob, idx), set(blob, idx)):
155186
// synatx sugar for get, set with []
156187

188+
// C-Strings / strings
189+
157190
// Use blob and struct to implement:
158191
// arrays
159192
// strings

0 commit comments

Comments
 (0)