본문 바로가기
프로그래밍/리눅스

gcc/g++ 분할 파일 컴파일 하기.

by Hwan2 2020. 5. 17.
728x90
반응형

C언어.


1. 함수 등록.


1
2
3
4
5
6
7
8
9
10
11
12
// test.c
 
#include <stdio.h>
 
int add(int a, int b);
 
int main(){
    printf("%d\n", add(12));    
 
    return 0;
}
 

cs



1
2
3
4
5
6
// add.c
 
int add(int a, int b){
    return a + b;
}
 
cs



명령어.


$ gcc test.c add.c

(오브젝트 파일 과정과 실행파일 지정 생략 가능)




2. 헤더 파일선언


1
2
3
4
5
6
7
8
9
10
11
// add.h
 
#ifndef _ADD_H_
#define _ADD_H_
 
#include <stdio.h>
 
int add(int a, int b);
 
#endif
 

cs


1
2
3
4
5
6
// add.c
 
int add(int a, int b){
    return a + b;
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
// test.c
 
#include <stdio.h>
#include "add.h"
 
int main(){
    printf("%d\n", add(12));    
 
    return 0;
}
 
cs


명령어.


$ gcc test.c add.c

(오브젝트 파일 과정과 실행파일 지정 생략 가능)




C++.


1
2
3
4
5
6
7
8
9
10
11
// test.cpp
 
#include <iostream>
 
int add(int a, int b);
 
int main(){
    std::cout << add(12<< std::endl;
 
    return 0;
}
cs


1
2
3
4
5
// add.cpp
 
int add(int a, int b){
    return a + b;
}
cs



명령어.


$ g++ test.cpp add.cpp

(오브젝트 파일 과정과 실행파일 지정 생략 가능)




헤더파일 선언


1
2
3
4
5
6
7
8
9
10
// add.hpp
 
#ifndef _ADD_H_
#define _ADD_H_
 
#include <iostream>
 
int add(int a, int b);
 
#endif
cs


1
2
3
4
5
// add.cpp
 
int add(int a, int b){
    return a + b;
}
cs


1
2
3
4
5
6
7
8
9
10
// test.cpp
 
#include <iostream>
#include "add.hpp"
 
int main(){
    std::cout << add(12<< std::endl;
 
    return 0;
}
cs


명령어.


$ g++ test.cpp add.cpp

(오브젝트 파일 과정과 실행파일 지정 생략 가능)

반응형

댓글


스킨편집 -> html 편집에서