0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語Advent Calendar 2018

Day 25

cでスタティックライブラリー

Posted at

#概要

cでスタティックライブラリーやってみた。
paizaでpythonで書いてみた。

#やってみた事。

print.c hello.cを書いて、
gccで、print.oとhello.oを作って
arで、libprint.a作って
gccで、hello.oとlibprint.aからa.out作って
実行まで。

#サンプルコード

import subprocess
import os

def gcc():
    p_file = 'print.c'
    test_p = """
#include <stdio.h>

void print(void)
{
	printf("Hello Wold ");
}
"""
    with open(p_file, 'w') as f:
        f.write(test_p)
    c_file = 'hello.c'
    test_c = """
extern void print(void);

int main(void)
{
	print();
	return 0;
}
"""
    with open(c_file, 'w') as f:
        f.write(test_c)
    os.system("gcc -c %s" % p_file)
    os.system("gcc -c %s" % c_file)
    os.system("ar rcs libprint.a print.o")
    os.system("gcc hello.o -L./ -lprint")
    result = subprocess.Popen('./a.out', stdout=subprocess.PIPE).communicate()[0]
    for i, v in enumerate(result[ : -1].split('\n')):
        print v

if __name__ == '__main__':
  gcc()

#成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?