2014-12-13 3 views
-9

로봇 시뮬레이션을하고 있고, 시뮬레이션에서 로봇을 제어하기 위해 플러그인이라고하는 파일에 C++ 코드를 써야합니다.프로그래밍 방식으로 make & CMake를 실행하는 방법

플러그인을 컴파일하려면 CMake를 사용하고 터미널에 명령을 작성합니다.

이제 내가 원하는 것은 유전 알고리즘을 사용하여 최상의 컨트롤러를 테스트하기 위해 많은 플러그인 (수백)을 차례로 컴파일하는 것입니다.

모든 플러그인을 컴파일 할 수있는 C++ 코드를 작성하고 싶습니다. 각 플러그인에 대해 수동으로 (즉, 터미널에서 CMake 명령을 작성) 할 수 없습니다.

+2

_automatically_ 무엇을 의미합니까? 예정된 시간? 또는 업데이트 또는 설치용 분산 패키지로 사용 하시겠습니까? 나는 네가 실제로 요구하는 것을 얻지 못한다. –

+0

이것은 무엇을 찾고있는 http://m.wikihow.com/Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux –

+0

필요하지 않습니다 c + + 파일입니다. 나는 cmake를 타이핑하고 터미널에서 make를 쓰는 대신에 파일에 명령을 쓰고이 파일이 실행되는 동안 런타임에 다른 C++ 파일을 컴파일 할 것입니다. – user1931907

답변

0

는 내가 완전히 질문을 이해 모르겠지만, 새로운 C++ 프로젝트 CMake에 의해 구성되는 만들 수있는 답변 :

에 자상을해야합니다. CMake는 C++ 코드에서 요구하는 모든 경로를 C++ 소스 파일에 작성합니다.

/CMakeLists.txt 
/PluginAlpha 
    /CMakeLists.txt 
    /... 
/PluginBeta 
    /CMakeLists.txt 
    /... 
/PluginGamma 
    /CMakeLists.txt 
    /... 
/main.cpp 

최상위 CMakeLists.txt이 할 수있는 :의 새로운 프로젝트는 다음과 같은 구조가 있다고 가정 해 봅시다 프로젝트를 구성하는 한 부분으로,

cmake_minimum_required(VERSION 3.0 FATAL_ERROR) 
project(Builder) 

set(Plugins PluginAlpha PluginBeta PluginGamma) 
foreach(Plugin ${Plugins}) 
    set(Contents "${Contents}\n {\"\\\"${CMAKE_BINARY_DIR}/Build${Plugin}\\\"\", \"\\\"${CMAKE_SOURCE_DIR}/${Plugin}\\\"\"},") 
endforeach() 
string(REGEX REPLACE ",$" "" Contents "${Contents}") 

file(WRITE "${CMAKE_BINARY_DIR}/always_updated/paths.hpp" "#ifndef PATHS_HPP_ 
#define PATHS_HPP_ 

#include <array> 
#include <string> 
#include <vector> 

const std::vector<std::array<std::string, 2>> paths = {${Contents} 
}; 

#endif // PATHS_HPP_ 
") 
configure_file("${CMAKE_BINARY_DIR}/always_updated/paths.hpp" "${CMAKE_BINARY_DIR}/generated/paths.hpp" COPYONLY) 

add_executable(Builder main.cpp "${CMAKE_BINARY_DIR}/generated/paths.hpp") 
target_include_directories(Builder PRIVATE "${CMAKE_BINARY_DIR}/generated") 

그래서를,이 정의하는 C++ 헤더를 기록 각 플러그인에 대한 빌드 및 소스 경로 모음 - 예 : 그런 다음 MAIN.CPP 수 있습니다

#ifndef PATHS_HPP_ 
#define PATHS_HPP_ 

#include <array> 
#include <string> 
#include <vector> 

const std::vector<std::array<std::string, 2>> paths = { 
    {"\"E:/build/BuildPluginAlpha\"", "\"E:/PluginAlpha\""}, 
    {"\"E:/build/BuildPluginBeta\"", "\"E:/PluginBeta\""}, 
    {"\"E:/build/BuildPluginGamma\"", "\"E:/PluginGamma\""} 
}; 

#endif // PATHS_HPP_ 

같은 것을 할 : 당신이 프로그램 "빌더"를 실행하면 이제

#include <cstdlib> 
#include <iostream> 
#include <string> 
#include <vector> 

#include "paths.hpp" // Generated by CMake during configuration 

struct Results { 
    std::string plugin_id; 
    int configure_result, build_result; 
}; 

int main() { 
    std::vector<Results> results; 
    for (const auto& p : paths) { 
    const std::string& build_folder = p[0]; 
    const std::string& source_folder = p[1]; 

    results.emplace_back(); 
    results.back().plugin_id = source_folder; 

    std::string command = "cmake -B" + build_folder + " -H" + source_folder; 
    results.back().configure_result = std::system(command.c_str()); 

    command = "cmake --build " + build_folder + " --config Release"; 
    results.back().build_result = std::system(command.c_str()); 
    } 

    std::cout << "\n\n"; 
    for (const auto& r : results) { 
    std::cout << "Results for " << r.plugin_id << ":\n"; 
    std::cout << " configure result: " << r.configure_result << '\n'; 
    std::cout << " build result:  " << r.build_result << '\n'; 
    } 
    return 0; 
} 

를, 그것을 구성하고 각 플러그인을 구축해야한다.

이것은 단순한 예입니다. CMake 명령에 추가 args를 전달해야하거나 std::system보다 나은 것을 사용하고 싶지만 일반적인 생각을 줄 수 있습니다. 귀하의 질문이 꽤 모호했기 때문에 위의 코드에 대한 모든 세부 사항을 다루지는 않았습니다. 더 구체적이고 더 많은 질문을 주시기 바랍니다.

+0

답변 해 주셔서 감사합니다. 사실 우분투 12.04를 사용하고 있으며 전에 CMake를 사용하지 않았습니다. 플러그인의 경우, 나는 main.cpp의 실행시이를 생성 할 것입니다. 그래서 그들은 당신의 아이디어를 시도 할 것입니다. 그리고 나서 더 구체적인 질문을 할 것입니다. – user1931907

+0

걱정하지 마십시오. CMake v3.0의 문서는 [here] (http://www.cmake.org/cmake/help/v3.0/index.html)입니다. – Fraser