C++

[C++] main.cpp 이외에 빌드하는 법

Guk-blog 2024. 6. 5. 18:18
728x90
반응형

보통 컴파일 설정하는 글을 보면

main.cpp에서 "Hello World" 정도를 출력만

가능한 정도의 설정만 알려주는 것 같다(내가 이해 못 한 것일 수도 있음)

 

이번에 설명할 내용은

#ifndef IRun_h
#define IRun_h

#include <string>

class IRun {
public:
    virtual void run() = 0;
    virtual ~IRun() = default;
};

#endif // IRun_h
#ifndef A_H
#define A_H

#include "IRun.h"

class A : public IRun {
public:
    void run() override;
};

#endif // A_H
#include "A.h"
#include "B.h"

int main(){
	A a;
    B b;
    a.run();
    b.run();
	return 0;
}

인 상태라고 가정했을 때 그냥 f5를 눌러버리면 

'undefined reference to `vtable for ~'가 발생할 것이다.

이 오류는 main.cpp만 컴파일하였고 링크나 다른 문제는 없더라도

해당 오류가 계속 발생해서 task.json을 수정하는 방법이나 기타 방법이 많은데

cmake를 활용한 방법을 소개하도록 하겠다.

1.cmake를 설치

https://cmake.org/download/

 

Download CMake

You can either download binaries or source code archives for the latest stable or previous release or access the current development (aka nightly) distribution through Git. This software may not be exported in violation of any U.S. export laws or regulatio

cmake.org

2. cmakelists.txt를 작성한다

cmake_minimum_required(VERSION 3.10.2)
cmake_policy(SET CMP0074 NEW)

project(
    TestProejct
    VERSION 0.1
    DESCRIPTION "테스트 프로젝트"
    LANGUAGES CXX)
    

    # 현재 디렉토리의 모든 소스 파일 가져오기
file(GLOB SOURCES "*.cpp")

# 실행 파일 생성 및 링크
add_executable(project ${SOURCES})

위의 파일에 대한 설명은

주석으로 남겨두었으니

필요하다면 참고하여 수정하면된다

 

위의 파일을 작성하자마자 configure가 성공했다면 3 과정을 스킵해도 된다.

3. kit 선택

1) ctrl + shift + p를 누름

2) CMake : Select a Kit 선택

3) visual studio ~ arm64 선택

 

4. Debug 실행

1) ctrl + shift + p

2) CMake : Debug 선택

 

성공했다면 이제 작성한 코드들이 잘 작동하는지 확인하면 된다.

하... CPP 진짜 

728x90
반응형