-
vim editor(vim 편집기) - 정적 라이브러리Linux/vim 편집기 2020. 7. 10. 08:53반응형This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#include <stdio.h> int calculate(int opnum, int opnds[], char opratior); void input(int* Popnd_cnt, int* operand, char* Poperator); int main(void) { int opnd_cnt; int* operand; char operator; int result; int* Popnd_cnt; char* Poperator; Popnd_cnt = &opnd_cnt; Poperator = &operator; operand = (int*)malloc(sizeof(int)); input(Popnd_cnt, operand, Poperator); result = calculate(opnd_cnt, (int*)operand, operator); printf("Operation result: %d\n", result); return 0; } void input(int* Popnd_cnt, int* operand, char* Poperator) { printf("Operand count: "); scanf("%d", Popnd_cnt); realloc(operand, sizeof(int) * (*Popnd_cnt)); for (int i = 0; i < (*Popnd_cnt); i++) { printf("Operand %d: ", i + 1); scanf("%d", &operand[i]); } printf("Operator: "); getchar(); //버퍼에 남아 있는 엔터를 받아주기 위해서 꼬오오오옥 필요하다 scanf("%c", Poperator); } int calculate(int opnum, int opnds[], char op) { int result = opnds[0], i; switch (op) { case '+': for (i = 1; i < opnum; i++) result += opnds[i]; break; case '-': for (i = 1; i < opnum; i++) result -= opnds[i]; break; case '*': for (i = 1; i < opnum; i++) result *= opnds[i]; break; } return result; } 위의 코드로 정적라이브러리를 이용한 컴파일을 해본다
main.c calc.c input.c common.h 우선 위의 4개의 c 파일로 분할해 준다.
| 정적 라이브러리에 포함해줄 c 파일 컴파일
정적 라이브러리에 포함해줄 calc.c 와 input.c 파일을 컴파일 해준다
***warning 부분은 아직 더 봐야 한다 ..검색중 .. 컴파일에는 문제 없음!
| 정적 라이브러리 생성
명령어 ar rscv 를 이용해 정적 라이브러리를 생성해준다.
lib( ).a 의 틀을 유지하고 ( ) 안에 라이브러리 이름을 넣어준다
| 정적 라이브러리를 포함한 컴파일
정적 라이브러리를 포함해서 test라는 이름의 실행 파일을 생성
-L./ -l( ) 를 이용해서 정적 라이브러리를 포함해준다 ( ) 안에는 지정한 라이브러리 이름을 넣어준다
| 실행 결과
완성!
| Makefile을 사용한 경우
Makefile vim 편집기를 이용해서 Makefile 매크로를 생성해준다
우선 make clean으로 파일들을 정리 해준다
make 명령어를 이용해서 컴파일을 해준다
**warning 검색중 .. 컴파일은 문제 없음!
이제 생성된 execute 실행 파일을 실행해본다
완서어어엉
728x90'Linux > vim 편집기' 카테고리의 다른 글
ctags (0) 2020.07.16 동적 라이브러리 (1) 2020.07.10 vim editor(vim 편집기) - Makefile을 이용한 분할 컴파일 (2) 2020.07.06 vim editor(vim 편집기) - 명령어 (0) 2020.07.06 vim editor(vim 편집기) - 분할 컴파일 (0) 2020.07.06