Skip to content

Commit 3e65796

Browse files
committed
add a dll example
1 parent 680534f commit 3e65796

5 files changed

Lines changed: 121 additions & 0 deletions

File tree

cpp/Dll/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
main

cpp/Dll/SortComponent.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "SortComponent.h"
2+
3+
extern "C" {
4+
void DECLSPEC sortNumbers(int *numbers, int size) {
5+
for (int i = 0; i < size - 1; ++i) {
6+
for (int j = 0; j < size - i - 1; ++j) {
7+
if (numbers[j] > numbers[j + 1]) {
8+
int temp = numbers[j];
9+
numbers[j] = numbers[j + 1];
10+
numbers[j + 1] = temp;
11+
}
12+
}
13+
}
14+
}
15+
}

cpp/Dll/SortComponent.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef SORT_COMPONENT_H
2+
#define SORT_COMPONENT_H
3+
4+
#if defined _WIN32 || defined __CYGWIN__
5+
#ifdef EXPORTING_DLL
6+
#ifdef __GNUC__
7+
#define DECLSPEC __attribute__((dllexport))
8+
#else
9+
#define DECLSPEC __declspec(dllexport)
10+
#endif
11+
#else
12+
#ifdef __GNUC__
13+
#define DECLSPEC __attribute__((dllimport))
14+
#else
15+
#define DECLSPEC __declspec(dllimport)
16+
#endif
17+
#endif
18+
#else
19+
#if __GNUC__ >= 4
20+
#define DECLSPEC __attribute__((visibility("default")))
21+
#else
22+
#define DECLSPEC
23+
#endif
24+
#endif
25+
26+
extern "C" {
27+
DECLSPEC void sortNumbers(int *numbers, int size);
28+
}
29+
#endif // SORT_COMPONENT_H

cpp/Dll/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "SortComponent.h"
2+
#include <iostream>
3+
4+
int main() {
5+
int numbers[10] = {5, 3, 8, 6, 2, 7, 4, 9, 1, 0};
6+
7+
sortNumbers(numbers, 10);
8+
9+
for (int i = 0; i < 10; ++i) {
10+
std::cout << numbers[i] << " ";
11+
}
12+
std::cout << std::endl;
13+
14+
return 0;
15+
}

cpp/Dll/makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
CXX := g++
2+
CXXFLAGS := -Wall -std=c++11
3+
4+
SRC := $(wildcard *.cpp)
5+
OBJ := $(SRC:.cpp=.o)
6+
DLL := libSortComponent.so
7+
EXEC := main
8+
9+
LIBDIR := .
10+
LDFLAGS := -L$(LIBDIR) -lSortComponent
11+
12+
all: $(DLL) $(EXEC)
13+
14+
$(DLL): SortComponent.o
15+
$(CXX) -shared -o $@ $^
16+
17+
$(EXEC): main.o
18+
$(CXX) -o $@ $^ $(LDFLAGS)
19+
20+
%.o: %.cpp
21+
$(CXX) $(CXXFLAGS) -c -o $@ $<
22+
23+
clean:
24+
rm -f $(OBJ) $(DLL) $(EXEC)
25+
26+
run: $(EXEC)
27+
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./$(EXEC)
28+
29+
.PHONY: all clean test

0 commit comments

Comments
 (0)