ããã¸ã§ã¯ããCã½ã¼ã¹ã³ã¼ããPythonããã±ã¼ã¸ã§ä½¿ãã
- 0. æ¦è¦
- 1. C language project å®è£
- 2. CMake å®è£
- 3. CMake ãç¨ãã Python å®è£
- 4. Python/C Wrapper ã® CMake å
- 5. Docker 㨠setup.py ã®æ´å
- 6. pyproject.toml ã®æ´å
- github.com
ï¼ï¼ï¼ C language project å®è£
ã¾ã python ã¨é¢ä¿ãªãã C è¨èªã®ã³ã³ãã¤ã«ãããã¨ããããå§ãããã ã¤ã¾ãã C è¨èªã¨ã㦠message é¢æ°ãå®è£ ãã ãããã©ã¤ãã©ãªã¨ãã¦å ±æã§ããå½¢ã«ãã¦ã å®éã« C è¨èªã®é¢æ°ã¨ãã¦å¼ã³åºãã¦ã¿ãã
- ããã¸ã§ã¯ããCã½ã¼ã¹ã³ã¼ããPythonããã±ã¼ã¸ã§ä½¿ãã
- ï¼ï¼ï¼ C language project å®è£
- 1. 調æ»
- 2. ãã£ã¬ã¯ããªæ§æ
- 3. ããã±ã¼ã¸ã¤ã³ã¹ãã¼ã«
- 4. test å·¥ç¨ (test.bash 解説)
- 5. build å·¥ç¨ (build.bash 解説)
- 6. ã¾ã¨ã
1. 調æ»
åèæç®ã¯ä»¥ä¸ã®éãã
- Cè¨èªã¼ã, å ±æã©ã¤ãã©ãªã®ä½æã¨å©ç¨æ¹æ³ - 2021/06
- tree ã³ãã³ãé¢é£
- koyo-miyamura, ç°¡åãªãã£ã¬ã¯ããªæ§æå³ãæ¸ã -- Qiita, 2018/07
- tnakamura, ãã£ã¬ã¯ããªæ§æå³ä½ãã®ã«ä¾¿å©ã ã tree ã³ãã³ã -- ãã·ã£ãã°, 2012/12
2. ãã£ã¬ã¯ããªæ§æ
1-c_lang_project/ âââ util.bash âââ build.bash âââ test.bash âââ src â  âââ myapp â  â  âââ main.c â  âââ mypkg â  âââ include â  â  âââ mypkg.h â  âââ mypkg.c âââ tests ã  âââ run_test.base.c ã  âââ run_test.base.h ã  âââ test_mypkg.c
bash ãã¡ã¤ã«ç¾¤ã«ã¯ãããã§è§£èª¬ããã³ãã³ããä¸æ¬ã§å®è¡ã§ããããã«è¨è¿°ããã¦ããã ãã ãã util.bash ã¯å¤æ°å®ç¾©ã¨ã¦ã¼ãã£ãªãã£ã®å®ç¾©ãªã®ã§èª¬æã¯çãã
3. ããã±ã¼ã¸ã¤ã³ã¹ãã¼ã«
$ sudo apt install build-essential
4. test å·¥ç¨ (test.bash 解説)
ãã¹ãé§åéçºãæ¨æ¦ãã¦ããã®ã§ããã¹ãããå®è£ ãã¦ãããã ãã ããç°¡åãªé¢æ°ã§ãããã¨ããããã C è¨èªã®ãã¹ããã¬ã¼ã ã¯ã¼ã¯ã¯å°å ¥ãè¨å®ããããããããªã®ã§ã æ製㮠assert ãã¹ããä½ãã
ãã ãããã¹ãã®çºè¦ã ã楽ã«ãã¦ããããã
run_test.base.c
㨠run_test.base.h
ãã
run_test.c
㨠run_test.h
ãèªåçæããããã«ãããã
æ¬è³ªé¨åã§ã¯ãªãã®ã§ããã®4ç« ã¯èªã¿é£ã°ãã¦ãæ§ããªãã æ¬æ°ã§ãããªãããããããã¹ããã¬ã¼ã ã¯ã¼ã¯ã使ãã¹ãã ã ç§ã¯ bash ã¯æ¯è¼ç使ãæ £ãã¦ãã®ã§ã bash è¸ã§ç°¡æãã¹ããã¬ã¼ã ã¯ã¼ã¯ãä½ãã
4.1. test.bash åå解説
list_test_function_names ã¯ãåãã¹ããã¡ã¤ã«ããã ãã¹ãç¨é¢æ°ãéãã¦ããããã®ã³ã¼ãã ã
function list_test_function_names() { for f in "${TESTS_DIR:?}"/test_*.c; do if [ ! -f "$f" ]; then echo "test file not found" exit 1 fi grep -E ^"int test_[a-zA-Z0-9_]+\(\s*\)" "$f" | while read line; do echo $line | sed -E "s/^.+(test_[a-zA-Z0-9_]+).+$/\1/" done done }
ã¾ããã¯ã¤ã«ãã«ã¼ã㧠tests ãã£ã¬ã¯ããªãã test_*.c
ã«ããããããã¡ã¤ã«ãåã£ã¦ããã
次ã«ããã®åãã¡ã¤ã«ä¸ããã int test_*()
ã®å½¢ã®ãã¹ãç¨é¢æ°ãåã£ã¦ããã
ããã¦ããã®ãã¡ã® test_*
é¨åããã¹ãåã¨ãã¦åºåããã
list_test_function_names ãå©ç¨ãã¦ã
run_test(test_*, "test_*");
ã¨ãã C è¨èªä¸ã§ãã¹ãç¨é¢æ°ãå¼ã¶ã³ã¼ããçæããã
function list_call_of_test_functions() { list_test_function_names | while read line; do echo "run_test($line, \"$line\");" done }
ãã® list_call_of_test_functionsãé¢æ°ãã
run_test.base.c
ã® // TEST_MAIN_BLOCK
ãç½®ãæããã
sed "s@// TEST_MAIN_BLOCK@$(list_call_of_test_functions)@" \ "${TESTS_DIR:?}/run_test.base.c" \ > "${TESTS_DIR:?}/run_test.c"
// (ç½®ãæãå: run_test.base.c) int main(int argc, char const *argv[]) { // TEST_MAIN_BLOCK return 0; } // (ç½®ãæãå¾: run_test.c) int main(int argc, char const *argv[]) { test_res_of_message(); return 0; }
run_test é¢æ°ã¯ run_test.base.c ä¸ã§å®ç¾©ããã以ä¸ã®ãããªé¢æ°ã§ããã ããã®ãã¹ããå®è¡ãã¾ããã¨ããæ¨ã®ã¡ãã»ã¼ã¸ã表示ãã¦ããã¹ããå®è¡ãã¦ã ãæåãã¾ãã/ã¨ã©ã¼ãèµ·ãã¾ãããã¨ä¼ããã ãã®é¢æ°ã ã
typedef int (*test_function)(); int run_test(test_function f, char *test_name) { int test_has_succeeded; fprintf(stderr, "[TEST] Test: %16s.\n", test_name); test_has_succeeded = f(); if (test_has_succeeded != 0) { fprintf(stderr, "[TEST] ERROR: %16s.\n", test_name); } fprintf(stderr, "[TEST] Success: %16s.\n", test_name); }
次ã«ã run_test.h ãä½ãã
ããã¯ã list_test_function_names ãå©ç¨ãã¦ã
int test_*());
ã¨ããããããä¸ã§ã®é¢æ°å®ç¾©ç¨ãçæããã
function list_def_of_test_functions() { list_test_function_names | while read line; do echo "int ${line}();" done }
æ¬å½ã¯å test_*.c ãã¡ã¤ã«ã«å¯¾å¿ãã .h ãããã¼ãã¡ã¤ã«ãä½ãã¹ãã ãããã
ããã§ã¯ãã®å·¥ç¨ã¯çç¥ãã¦ããã¹ã¦ã®ãã¹ãç¨é¢æ°ã®å®ç¾©ã run_test.h
ã«éç´ããã
ãã®é¢æ°ã§ã run_test.base.h ã¯ä»¥ä¸ã®ããã«ç½®ãæããããã
sed "s@// TEST_MAIN_BLOCK@$(list_def_of_test_functions)@" \ "tests/run_test.base.h" \ > "${TESTS_DIR:?}/run_test.h"
// (ç½®ãæãå: run_test.base.h) #ifndef __RUNTEST_HEADER__ #define __RUNTEST_HEADER__ // TEST_MAIN_BLOCK #endif // (ç½®ãæãå¾: run_test.h) #ifndef __RUNTEST_HEADER__ #define __RUNTEST_HEADER__ int test_res_of_message(); #endif
ãã¼ãã C è¨èªãã¡ã¤ã«ãããã¹ããã¡ã¤ã«ã¨ãã¦ç¡çããå¦çããå¼·å¼ãã æ¬çªããã¸ã§ã¯ãã§çä¼¼ãããã¨ã¯ãªãã ããã
4.2. test.bash å¾å解説
å¾è¿°ãã build.bash ã使ã£ã¦ã
include "mypkg.h"
ã¨ãã¦ã¤ã³ã¯ã«ã¼ãå¯è½ãªå
±ç¨ã©ã¤ãã©ãªã§ãã libmypkg.so
ãä½æããã
ããã使ã£ã¦ãå
ç¨èªåçæãã run_test.c
ãã³ã³ãã¤ã«ãã
(../.build ãã£ã¬ã¯ããªã¯ã build.bash ã§èªåçæãããä¸æãã£ã¬ã¯ããªã§ãã)ã
cd tests gcc -I../.build/mypkg/include -o run_test \ test_*.c run_test.c ../.build/mypkg/libmypkg.so
ãã㧠run_test
ãçæãããã®ã§ããã¹ããå®è¡ããæºåãæ´ã£ãã
./run_test
ã§ã¯ãå®éã«ãã¹ããæ¸ããã
tests/test_mypkg.c
ã¨ãã¦ã 以ä¸ã®ãã¹ãç¨é¢æ°ãæ¸ãã¦ã¿ãã
#include <assert.h> #include "mypkg.h" int test_res_of_message() { int res; res = message(0); assert(res == 0); }
message ãæ¨æºåºåããå 容ãè¦ãã®ã¯é¢åãªã®ã§ã å¼æ°ã« 0 ã¨ä¸ããããè¿ãå¤ã 0 ã«ãªãä»æ§ã¨ãã¦ããã®ãã¹ããæ¸ããã
assert()
é¢æ°ã¯ãå¼æ°ã®ä¸èº«ã true ãªãä½ãããã
false ãªãã¨ã©ã¼ã¨ãã¦ããã°ã©ã ãçµäºããé¢æ°ã§ããã
ãã¾ãè¡åã®è¯ãé¢æ°ã§ã¯ãªãããè¡æ°ãå¼æ°ã®ä¸èº«ãªã©ã
æä½éã®æ
å ±ãæããªããçµäºãã¦ãããã
å®éã«ã¨ã©ã¼ãåºãã¨ã以ä¸ã®ããã«è¡¨ç¤ºãããã
[TEST] Test: test_res_of_message. run_test: test_mypkg.c:9: test_res_of_message: Assertion `res == 0' failed. ./test.bash: line 49: 16075 Aborted ./run_test
ãã®ããã«ãç°¡æçãªãã¹ããè¡ãã«ã¯ååã ã
ããã¦ãç¾æç¹ã§ã¯ libmypkg.so
ãç¡ããã gcc ã¯éããªããã
ãããã³ã³ãã¤ã«ãã build.bash ãç¡ããã°ã
ããããã³ã³ãã¤ã«å¯¾è±¡ã®ã³ã¼ããæ¸ãã¦ããªãã
ããããå§ãã¦ãããã¨ã«ãããã
5. build å·¥ç¨ (build.bash 解説)
ããã§ã¯ãæ¬é¡ã«æ»ããã ç®æ¨ã¯ä»¥ä¸ã®éãã ã£ãã
C è¨èªã¨ã㦠message é¢æ°ãå®è£ ãã ãããã©ã¤ãã©ãªã¨ãã¦å ±æã§ããå½¢ã«ãã¦ã å®éã« C è¨èªã®é¢æ°ã¨ãã¦å¼ã³åºãã¦ã¿ãã
ããã§ã¯ã message é¢æ°ã mypkg
ã¨ããååã®å
±æã©ã¤ãã©ãªã«ããã
ããã¦ãå¥ã® myapp
ã¨ããã¢ããªã±ã¼ã·ã§ã³ããå¼ã³åºããã¨ã«ãããã
5.1. Build mypkg
ã¾ãã mypkg.h
ãæ¸ããã
message() ã¨ããé¢æ°ãä½ãããã®ã ã£ãã
#ifndef __MYPKG_HEADER__ #define __MYPKG_HEADER__ int message(int res); #endif
次ã«ã mypkg.c
ãæ¸ããã
message() ã§ã¯ã "Hello world!" ã¨è¡¨ç¤ºãã¦ãããã
#include <stdio.h> int message(int res) { printf("Hello world!\n"); return res; }
C è¨èªã®ã³ã³ãã¤ã«ã¯ãè¨ããã¨ããã gcc ã³ãã³ãã«ãã£ã¦è¡ãã
以ä¸ã®ã³ãã³ãã§ãå
±æã©ã¤ãã©ãª libmypkg.so
ãä½æã§ããã
ç´°ãããªãã·ã§ã³ã®è§£èª¬ã¯ã5.3. å種ãªãã·ã§ã³ã®è§£èª¬ãã®ç¯ã«è²ãã
$ cd src/mypkg $ gcc -Iinclude -shared -fPIC -o libmypkg.so mypkg.c
libmypkg.so
ã¯ãä»ã®ããã°ã©ã ããå
±æã©ã¤ãã©ãªã¨ãã¦ä½¿ããã
è¨ãæããã°ãinclude "mypkg.h"
ã¨ãã¦ããã°ã
誰ã§ã message()
é¢æ°ãå¼ã³åºããã¨ãããã¨ã«ãªãã
5.2. Build myapp
ä¾ã¨ãã¦ã次ã®ãã㪠myapp.c
ãã src/myapp ã«ä½ããã
#include <stdio.h> #include "mypkg.h" int main(int argc, char const *argv[]) { message(0); }
myapp.c
ã¯ã #include mypkg.h
㧠message()
é¢æ°ãå¼ã¹ãããã«ãã¦ããã
ããã¦ããããã³ã³ãã¤ã«ããã«ã¯ã以ä¸ã®ããã«ããã°è¯ãã
$ cd myapp $ gcc -I../mypkg/include -o myapp main.c ../mypkg/libmypkg.so
å ç¨ã®å ±æã©ã¤ãã©ãªã¨ã include ç¨ã®ããããã¡ã¤ã«ãå ¥ã£ããã£ã¬ã¯ããªãæå®ãã¦ã ã³ã³ãã¤ã«ããã å®éã«å®è¡ããã¨ã以ä¸ã®ããã«ãªãã
$ ./myapp Hello world!
5.3. å種ãªãã·ã§ã³ã®è§£èª¬
5.3.1. -I 㨠-l ãªãã·ã§ã³
-I
ã¯ã¤ã³ã¯ã«ã¼ããã£ã¬ã¯ããªãæå®ãããªãã·ã§ã³ã§ããã
-l
ã¯ã©ã¤ãã©ãªãæå®ãããªãã·ã§ã³ã§ããã
ã ããããããã¤ã³ã¯ã«ã¼ããã¡ã¤ã«ã¨ããããã¼ãã¡ã¤ã«ã¯ãªãåå¨ããã®ãã
ãªãããããã©ã¤ãã©ãªãä½ã£ã¦ãããã¤ã³ã¯ã«ã¼ãããã®ãã
èªèº«ã§å¿ããã¡ãªã®ã§æ´çãã¦ããã
çµè«ãå ã«ããã¨ãããã¢ããªã±ã¼ã·ã§ã³ãã©ã¤ãã©ãªãããã¨ãã ãããã¼ãã¡ã¤ã«/ã¤ã³ã¯ã«ã¼ããã¡ã¤ã«ã¨ããã®ã¯ã ãã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®ç¾©ãããã®ã§ãã (ããã¯ç§èªèº«ã®çµé¨ãããããã¹ãã¨æããã¨ããã ããªã®ã§èª¤ããããããªã)ã
åè¿ãªä¾ã§å©ãã¦ã¿ãããä¾ãã°ããç飯å¨ãã¨ããæ©æ¢°ãèãã¦ã¿ãã ãããã¢ããªã±ã¼ã·ã§ã³ã ã¨è¦åãã¨ã ãç飯å¨ãã«ã¯ãç飯ãã¿ã³ããã¤ãã¦ããã ãããã¤ã³ã¿ã¼ãã§ã¼ã¹(å°é£ããè¨ãã°ãå¤é¨åãã«å ¬éãããæä½ç¨ã®è£ ç½®)ãä»ãã¦ããã ä¸æ¹ã§ããç飯å¨ãã«ã¯ãç飯æ©è½ããã¤ãã¦ããã®ã ãã ç§éã¯ã©ããªãµãã«ç飯ãè¡ããã¦ãããã詳細ã«ã¤ãã¦ã¯ç¥ããªãã ãããããã°ã©ã 風ã«è¨ãã°ã ããã®æ©è½ã®è©³ç´°ãªå®è£ ãã©ã®ããã«ãªããã¦ããã(å é¨ä»æ§)ã¯å ¬éããã¦ããªããã¨ãªãã
ç飯å¨ã®ä¾ã«éããã æ´æ¿¯æ©ã¯ããã¿ã³ãæ¼ãããæ´æ¿¯ãããã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ ã(ã¿ãåã ããããã©ã å¼ã ããã)ä¸ã®ãã®ãæ´æ¿¯ãããã¨ããå®è£ ãæã£ã¦ããã ãã¬ãã¯ããªã¢ã³ã³(ãã¿ã³ãæ¼ããããã£ã³ãã«ãå¤ãã)ãã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ ãé»æ³¢ãåãåã£ã¦æå®ããããã£ã³ãã«ã®æ åã表示ãããã¨ããå®è£ ãæã£ã¦ããã ã¹ãã¼ããã©ã³ã¯ãé»è©±çªå·ãæ¼ãããç¸æã«é»è©±ãããããã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ ãç¡ç·éä¿¡è¦æ ¼ã«å¾ã£ã¦æå®ãããé»è©±çªå·ã®ç¸æã¨æ¥ç¶ãããã¨ããå®è£ ãæã£ã¦ããã ãããããã¦ã¼ã¶ã¯èªèº«ã«å ¬éãããã¤ã³ã¿ã¼ãã§ã¼ã¹(=説ææ¸ã«æ¸ãã¦ãããããªãã¨)ã ãç解ãã¦ããã°ããã å é¨ã®ç´°ãããã¨ãç¥ãå¿ è¦ããªãã
ãããããã°ã©ãã³ã°ã«ãããã«ãã»ã«åã å°éç¨èªã§è¨ãã° Open-Closed ã®ååã¨å¼ã°ãããã®ã§ããã ãã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨å®è£ ã®åé¢ãä¸æãããã¨ã æ©è½ã使ãå´ãããã¹ããã¨ãå¤ããã«ããã®ä¸èº«ã ããæ°ããã§ããã ããã¦ãå¤è¦ãå¤ããã«ä¸èº«ã ãæ´æ°ã§ããã¨ããã®ã¯ã è³ç£ã®åå©ç¨ãç½®ãæãããããããã¨ããå©ç¹ãããã
ä¾ãã°ãæè¿ãã¼ãã¹ãå ¥ã£ããããç飯å¨ãæ°ãããã¦ç½ç±³ã®ã¯ãªãªãã£ãä¸ããããã¨æã£ãã¨ãã ããªããããã¹ããã¨ã¯ç飯å¨ãã°ã¬ã¼ãã¢ãããã¦ããã¤ãã¨åãããã«ãç飯ãã¿ã³ããæ¼ãã ãã§ããã
éã«ããåéã§ç±³ãçãã¨ç¾å³ãããªãããããã¨èãããã試ãã¦ã¿ãããã¨ããå ´åã«ã¯ã ããªãã¯åéã使ã£ã¦ã©ããã£ã¦ç±³ãçããä¸ãã調ã¹ã¦ã éã«ç±³ã¨æ°´ãå ¥ããç¦ããªãããã«ç«å æ¸ã¨ç±ããæéã管çãâ¦â¦ ã¨ãã£ãããã¤ãã¨ã¯å ¨ãéããã¨ãããªããã°ãªããªããªãã
æçã¸ã®ç±æãããã°ãããããã¨ãã§ãããããããªããã æ©è½ã®ã¢ããã°ã¬ã¼ãã®åº¦ã«ãªãã§ãããã§ããããªãã¨ããã¦ãããã 人çã¨ããã®ã¯ç¸å½è¤éã«ãªã£ã¦ãã¾ãã ããã å®éããã ã®ç飯å¨ã§ããä»ãç飯ã¢ã¼ããããããããããã ã¡ã¼ã«ã¼ãå¤ããã ãã§ããåãæ©æ¢°ã¨ã¯æããªããã¨ãã人ããããããããªãã
é話ä¼é¡ã
ããã°ã©ã ã§ã¯ãä¾ãã°ããã©ã¼ãã³ã¹ãã¥ã¼ãã³ã°ã¨ã ã»ãã¥ãªãã£ã¢ãããã¼ãã¨ãã®ç´°ããªã¢ãããã¼ããããããããªãã éã«ãããããæ©è½è¿½å ã®ãã³ã«éå»ã®æ©è½ã使ããªããªããã¨ãç¹°ãè¿ãããã¨ã ãã®æ©è½ã使ã人ããããã¨æºã¾ã£ããã®ã§ã¯ãªãã ä¸åº¦ããããã¼ã¸ã§ã³ã® PlayStation ã®ã½ãããå¾ç¶ãã¼ãã§ä½¿ããªããªããããªãã®ã ã ãããã¯ãã¢ãããã¼ãã®åº¦ã«ä»æ§ãå¤ãã£ã¦åã¨åãããã«ä½¿ããªããªã OS ã«ã¤ãã¦ãä¼¼ããããªãã¨ãè¨ãã (Windowsããåã®ãã¨ã )ã
ãã®ãããèªåãããã°ã©ã ãä½ãéãã å¤é¨ã«å ¬éããé¢æ°ã®å®ç¾©(ã·ã°ããã£)ã ãããããã¼ãã¡ã¤ã«ã«ã¾ã¨ãã ãããæ´ã«ã¤ã³ã¯ã«ã¼ããã£ã¬ã¯ããªã«ã¾ã¨ããããã«ããã ãããããã¨ã§ããã®ããã°ã©ã ã使ããã¨ãã人ã¯ã ãã® .h ã®ä¸èº«ã ãèªãã°ãããªããä½ã£ã .c ã®ä¸èº«ã¾ã§ã¯ç¥ããªãã¦ãã ããã°ã©ã ã使ããã¨ãã§ããããã«ãªãã è¨ãæããã°ãã¤ã³ã¯ã«ã¼ããã£ã¬ã¯ããªã説ææ¸ã®ããã«ä½¿ããã¨ãã§ããã®ã§ããã
ã¨ã¯ãããã³ã³ãã¤ã«æã«ã¯ãå®éã«ãã®ä¸èº«ãå®è¡ãã¦ããããªããã°ãªããªãã ãã®ããã«ã¯ã使ãå´ã¯ .c ã½ã¼ã¹ã³ã¼ãã§æ¸ãããå®è£ ãåãåããªããã°ãªããªãã ãã®ã¨ãããã® .c ãç´æ¥æ¸¡ãã¦ãããã®ã ãã è¤æ°ã® .c ãã¡ã¤ã«ã®å®è£ ãã¾ã¨ããä¸ã¤ã®ãã©ã¤ãã©ãªãã¨ãã¦æ¸¡ãã¦ããããã¨ã 使ãå´ã§ã³ã³ãã¤ã«ããªãã¦ããããã楽ã ã ã¾ãã .c ã®æ°ã«å¢æ¸ããã£ãå ´åã«ããã®å½±é¿ãåããã«æ¸ãã ä»ã«ããä¼æ¥æ¦ç¥çã«ã¯ã .c ã®ä¸èº«ãä¼æ¥ç§å¯ã¨ããå ´åãããã ããã ãã®å ´åã¯ãã©ã¤ãã©ãªãã¨ã㦠.c ã®ä¸èº«ãé è½ãã¦ããããã¨å©ããã
ãããã£ã諸ã ã®çç±ã§ãèªåãä½ã£ãããã°ã©ã ã«ã¤ãã¦ã
- ãå¤è¦(ã¤ã³ã¿ã¼ãã§ã¼ã¹)ãã¯ãããã¼ãã¡ã¤ã«/ã¤ã³ã¯ã«ã¼ããã£ã¬ã¯ããªã«ã¾ã¨ããã
- ãä¸èº«(å®è£ )ãã¯ã©ã¤ãã©ãªã«ã¾ã¨ããã
ã¨ããã®ãéä¾ã¨ãªã£ã¦ãããã®ã ã¨æãã
5.3.2. åçã©ã¤ãã©ãªã»éçã©ã¤ãã©ãªã»å ±æã©ã¤ãã©ãª
ã©ã¤ãã©ãªã«ã¯ããã¤ãã®ç¨®é¡ãããã ãããããããããå¿ããã¡ãªã®ã§ãããã«ã¤ãã¦ãç°¡åã«ã¾ã¨ãã¦ããã
ã¾ããåçã©ã¤ãã©ãªã¨ãéçã©ã¤ãã©ãªã¨ãããã ãããã¯ãã©ã¤ãã©ãªã®ãªã³ã¯æ¹æ³ãã«ãã£ã¦åºå¥ããã¦ããã æ£ç¢ºã«ã¯ãåçãªã³ã¯ãã¨ãéçãªã³ã¯ãã¨ããã®ãæ£ããã ãåç (dynamic)ãã¨ãéç (static)ãã¨ããã®ã¯è±èªçãªè¡¨ç¾ã§ããã é çªãåå¾ãããããéçãã¨ããè¨èã®ã¤ã¡ã¼ã¸ã¯ã ãæåããå ¨é¨å®æãã¦ãã以éå¤ãããã¨ããªããã¨ãããããªãã®ã ã éã«ããåçãã¨ããè¨èã®ã¤ã¡ã¼ã¸ã¯ã ã使ããã¨ããã¨ãã«å¿ è¦ã«å¿ãã¦æ©è½ãä»ãå ãã¦ãããã¨ãããããªãã®ã«ãªãã
ããããã©ã¤ãã©ãªã®ãªã³ã¯æ¹æ³ãã«å½ã¦ã¯ããã¨ã以ä¸ã®ããã«ãªãã
- éçãªã³ã¯ã©ã¤ãã©ãª: ã©ã¤ãã©ãª/ããã°ã©ã ãã³ã³ãã¤ã«ããã¨ã(=æåã«ä½ãã¨ã)ã«ä½¿ãç¨ã®ã©ã¤ãã©ãªã
- åçãªã³ã¯ã©ã¤ãã©ãª: ã©ã¤ãã©ãª/ããã°ã©ã ãå®è¡ããã¨ãã«ä½¿ãç¨ã®ã©ã¤ãã©ãªã
gcc
ãå®è¡ããã¨ãã«ã©ã¤ãã©ãªãæå®ãããå ´åã¯ãéçã©ã¤ãã©ãªãç¨ããã
ä¸æ¹ãããã°ã©ã ãå®è¡ããéã«ã©ã¤ãã©ãªãæå®ãããå ´åã¯ãåçã©ã¤ãã©ãªãç¨ãã
(ãã®éãã©ã¤ãã©ãªã®å©ç¨æ¹æ³ã¨ãã¦ãåçãªã³ã¯ã¨åçãã¼ãã£ã³ã°ã¯æ··åãããã¡)ã
æ®éã«ã©ã¤ãã©ãªã使ãå ´åãããããæèãããã¨ã¯ãã¾ãç¡ãã¨æãã
é常ã¯éçãªã³ã¯ã§æ¸ãã¨æãããå®è¡æã«ããã°ã©ã ã®å®è£
ãããã£ã¨å
¥ãæ¿ãããå ´åã«ã¯ã
åçãªã³ã¯ãç¨ãããã¨ã«ãªãã
å ±æã©ã¤ãã©ãªã¨ããã®ã¯ã大ä½åçãªã³ã¯ã¨ä¸ç·ã«ä½¿ãããããå³å¯ã«ã¯ç°ãªãæ¦å¿µã§ããã å ±æã©ã¤ãã©ãªã¨ããè¨èãããã¨ãããã¨ã¯ã ãå ±æã§ãªãã©ã¤ãã©ãªããããã¯ãéå ±æã©ã¤ãã©ãªãã¨ã§ãããã¹ããã®ãããã
ä¾ãã°ãããªãããããã³ã®è¨åãæ´ãããã¨ãã¦ããã¨ããã ãã®ã¨ãã大ä½ã³ã³ããæ°´éã¯åãä»ããã®ã使ããã¨ã«ãªãã ãããã¯ä»ã®ãããã³ã¨ãå ±æããããã¨ãã§ããªãã ä¸æ¹ãç飯å¨ãå·èµåº«ã¯ãå¼ã£è¶ãåã«ä½¿ã£ã¦ãããã®ã使ã£ã¦ãããã ããããæå³ã§ãä»ã®ãããã³ã¨ãå ±æããããã¨ãã§ããã
ããã°ã©ã ã§ãåããããªäºæ ãããã ä¾ãã°ããªããããã°ã©ã ã3ã¤ã®ã©ã¤ãã©ãªã«åãã¦ã³ã³ãã¤ã«ãããã¨ããã¨ãã ããªãèªèº«ããã®ã©ã¤ãã©ãªã使ããã°ããã ããªãã ããã°ã©ã å ã§ã®ã©ã¤ãã©ãªã®é ç½®ãåºå®(=åãä»ã)ãã¦ããã°ããã
ããããä»ã®äººã®ä½ã£ãã©ã¤ãã©ãªã使ãå ´åã«ã¯ã ãã®äººã®ããã°ã©ã ã«åãä»ããããã©ã¤ãã©ãªã«ãªã£ã¦ããã¨ä½¿ãç©ã«ãªããªãã ããã¯ç¡çããä¾ããã°ãä»ã®å®¶ã«åãä»ãããããããã³ãå¼ã£å¥ãã㦠ç¡çããèªåã®å®¶ã®ãããã³ã¹ãã¼ã¹ã«å ¥ãããããªãã¨ã«ãªãã ããã°ã©ã ã®å ´åã¯ã大æµã®å ´åã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªããã ããã©ã¼ãã³ã¹ãæªåãã(ãããã試ãããã¨ã¯ãªã)ã
ãã®ãããä»ã®äººã§ã使ãããç飯å¨ãå·èµåº«ã®ããã«ããããã«ã¯ã
ããããå
±æå¯è½(=ã©ãã«ã§ãç½®ãã)ããã®ã¨ãªã£ã¦ããå¿
è¦ãããã
ãããã³ã³ãã¤ã«æã«æå®ãã¦ããã®ãã-shared -fPIC
ãªãã·ã§ã³ã§ããã
ãããæå®ããªãå ´åã¯ãåã«åå²ã³ã³ãã¤ã«ã®ããã«ã©ã¤ãã©ãªãä½ããã¨ã«ãªãã
6. ã¾ã¨ã
ãã®ç« ã§ã¯ã C è¨èªã«ãããå ±æã©ã¤ãã©ãªã®ä½ãæ¹ã»ä½¿ãæ¹ã®å¾©ç¿ãè¡ã£ãã include ãã©ã¤ãã©ãªãåçã©ã¤ãã©ãªã¨éçã©ã¤ãã©ãªãªã©ã ãã®æç¹ã§ãããããæ¦å¿µãå¤ãåºã¦ãããã åºæ¬ã®ãããã§ãããã®ã§éè¦ãªã¨ããã§ãããã