2014-11-23 39 views
0

我得到了一個基於C的主項目,我需要包含一個CPP功能(使用OpenCV)。 我正在使用Cmake文件來這樣做,但是當我添加cpp文件時,這不起作用。混合CPP OpenCV /主程序C

這裏我的Makefile

cmake_minimum_required (VERSION 2.8.9) 

project(tictactoe) 

### Requierements 

    find_package(OpenCV REQUIRED) 
    include_directories(../Sources) 
include_directories(${OpenCV_INCLUDE_DIRS}) 
    include(CheckIncludeFiles) 
include(CheckIncludeFileCXX) 
### Subdirectories where the sources are 

    FILE(GLOB MyCSources "../Sources/*.c") 
ADD_LIBRARY(detect_tag ../Sources/detect_tag.cpp ../Sources/detect_tag.hpp) 
ADD_EXECUTABLE(tictactoe ${MyCSources} ../Sources/detect_tag.cpp ../Sources/detect_tag.hpp) 

TARGET_LINK_LIBRARIES(tictactoe rt ${CMAKE_THREAD_LIBS_INIT} ${OpenCV_LIBS}) 
+0

不是你的主要問題,但我很確定'* .hpp'文件不需要在源文件中列出:它們沒有被編譯,如果它們沒有被包含,那麼它們就不會被使用。 ..所以cmake不需要知道他們。 – dave 2014-11-23 21:36:58

回答

0

好吧,我使用的包裝解決我的問題: 我做喜歡wrapper.h和wrapper.cpp的頭文件。

在wrapper.h我得到:

extern "C" my_function_through_wrapper(); 
在wrapper.c我得到

#include file_where_my_function_if.h 
extern "c" { 

my_function_through_wrapper(){ 
     my_function(); 
} 

} 

就是這樣! 的確,我已經在真正的CPP和C代碼之間建立了接口。通過extern C,我的編譯器知道如何編譯包裝文件,以便我的C代碼可以鏈接到它。然後通過封裝器,CPP可以使用CPP中包含的所有庫管理器鏈接到其他CPP代碼。解決了 !