Skip to content

Commit 6cf0049

Browse files
author
liuxinwei
committed
更新文档
1 parent 2d12f38 commit 6cf0049

File tree

8 files changed

+309
-7
lines changed

8 files changed

+309
-7
lines changed

doc/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,7 @@ dmypy.json
133133

134134
# breathe
135135
xml/
136-
draft/
136+
draft/
137+
138+
# temp
139+
.temp/

doc/exercises/c2cpp.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
{
3636
"cell_type": "code",
37-
"execution_count": 1,
37+
"execution_count": null,
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [

doc/exercises/cpp2c.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
" add( 2, 3 ); \n",
4242
" return 0;\n",
4343
"}\n",
44-
"``"
44+
"```"
4545
]
4646
},
4747
{

doc/exercises/fopen.ipynb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# `fopen`"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {
14+
"vscode": {
15+
"languageId": "c++"
16+
}
17+
},
18+
"outputs": [],
19+
"source": [
20+
"#include <cstdio>\n",
21+
"\n",
22+
"int main() {\n",
23+
" FILE* file = fopen(\".temp/example.txt\", \"w\");\n",
24+
" if (file) {\n",
25+
" fputs(\"Hello World\", file);\n",
26+
" fclose(file); // 必须关闭文件句柄\n",
27+
" }\n",
28+
" return 0;\n",
29+
"}"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 2,
35+
"metadata": {
36+
"vscode": {
37+
"languageId": "c++"
38+
}
39+
},
40+
"outputs": [
41+
{
42+
"data": {
43+
"text/plain": [
44+
"0"
45+
]
46+
},
47+
"execution_count": 2,
48+
"metadata": {},
49+
"output_type": "execute_result"
50+
}
51+
],
52+
"source": [
53+
"main()"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"metadata": {
60+
"vscode": {
61+
"languageId": "c++"
62+
}
63+
},
64+
"outputs": [],
65+
"source": []
66+
}
67+
],
68+
"metadata": {
69+
"kernelspec": {
70+
"display_name": "C++14",
71+
"language": "C++14",
72+
"name": "xcpp14"
73+
},
74+
"language_info": {
75+
"codemirror_mode": "text/x-c++src",
76+
"file_extension": ".cpp",
77+
"mimetype": "text/x-c++src",
78+
"name": "C++14",
79+
"version": "14"
80+
}
81+
},
82+
"nbformat": 4,
83+
"nbformat_minor": 2
84+
}

doc/exercises/include/mylib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef MYLIB_H
2+
#define MYLIB_H
3+
4+
int multiply(int a, int b);
5+
6+
#endif

doc/exercises/src/mylib.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "mylib.h"
2+
3+
int multiply(int a, int b) {
4+
return a * b;
5+
}

doc/exercises/xeus-cling-c.ipynb

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# `xeus-cling` 调用 C 代码"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## 直接内联 C 代码"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"// 使用 extern \"C\" 包裹 C 代码\n",
24+
"extern \"C\" {\n",
25+
" // C 语言函数定义\n",
26+
" int add(int a, int b) {\n",
27+
" return a + b;\n",
28+
" }\n",
29+
"}"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 2,
35+
"metadata": {},
36+
"outputs": [
37+
{
38+
"data": {
39+
"text/plain": [
40+
"7"
41+
]
42+
},
43+
"execution_count": 2,
44+
"metadata": {},
45+
"output_type": "execute_result"
46+
}
47+
],
48+
"source": [
49+
"// 在 C++ 中调用\n",
50+
"int result = add(3, 4);\n",
51+
"result // 在 Notebook 中显示结果"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"## 调用预编译的 C 库"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"创建 C 头文件 `mylib.h`"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 3,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"name": "stdout",
75+
"output_type": "stream",
76+
"text": [
77+
"Overwriting include/mylib.h\n"
78+
]
79+
}
80+
],
81+
"source": [
82+
"%%file include/mylib.h\n",
83+
"#ifndef MYLIB_H\n",
84+
"#define MYLIB_H\n",
85+
"\n",
86+
"int multiply(int a, int b);\n",
87+
"\n",
88+
"#endif"
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"创建 C 源文件 `mylib.c`:"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 4,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"name": "stdout",
105+
"output_type": "stream",
106+
"text": [
107+
"Overwriting src/mylib.c\n"
108+
]
109+
}
110+
],
111+
"source": [
112+
"%%file src/mylib.c\n",
113+
"#include \"mylib.h\"\n",
114+
"\n",
115+
"int multiply(int a, int b) {\n",
116+
" return a * b;\n",
117+
"}"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"编译为共享库:"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 5,
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"!gcc -shared -fPIC -o .temp/libmymath.so src/mylib.c -Iinclude"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"metadata": {},
139+
"source": [
140+
"在 `xeus-cling` 中调用:"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 6,
146+
"metadata": {},
147+
"outputs": [],
148+
"source": [
149+
"// 加载动态库\n",
150+
"#pragma cling add_library_path(\".temp\") // 指定库路径\n",
151+
"#pragma cling load(\"mymath\") // 加载 libmymath.so\n",
152+
"\n",
153+
"// 声明 C 函数\n",
154+
"extern \"C\" {\n",
155+
" int multiply(int a, int b);\n",
156+
"}"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 7,
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"data": {
166+
"text/plain": [
167+
"30"
168+
]
169+
},
170+
"execution_count": 7,
171+
"metadata": {},
172+
"output_type": "execute_result"
173+
}
174+
],
175+
"source": [
176+
"// 调用函数\n",
177+
"multiply(5, 6) // 输出 30"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": null,
183+
"metadata": {},
184+
"outputs": [],
185+
"source": []
186+
}
187+
],
188+
"metadata": {
189+
"kernelspec": {
190+
"display_name": "C++14",
191+
"language": "C++14",
192+
"name": "xcpp14"
193+
},
194+
"language_info": {
195+
"codemirror_mode": "text/x-c++src",
196+
"file_extension": ".cpp",
197+
"mimetype": "text/x-c++src",
198+
"name": "c++",
199+
"version": "14"
200+
}
201+
},
202+
"nbformat": 4,
203+
"nbformat_minor": 2
204+
}

doc/start/class/start.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,16 @@
162162
"hash": "a2954483fe4136a94bd721da8601251ae4c667fedb6adcfe70ff0183b19bacbc"
163163
},
164164
"kernelspec": {
165-
"display_name": "C++11",
166-
"language": "C++11",
167-
"name": "xcpp11"
165+
"display_name": "C++14",
166+
"language": "C++14",
167+
"name": "xcpp14"
168168
},
169169
"language_info": {
170170
"codemirror_mode": "text/x-c++src",
171171
"file_extension": ".cpp",
172172
"mimetype": "text/x-c++src",
173173
"name": "c++",
174-
"version": "11"
174+
"version": "14"
175175
}
176176
},
177177
"nbformat": 4,

0 commit comments

Comments
 (0)