CMake 創建靜態庫及動態庫供函數調用,下面是項目的樹狀圖
.
├── cmake_fei
│ ├── build
│ ├── CMakeLists.txt
│ ├── doc
│ │ ├── readme.txt
│ │ └── CMakeLists.txt
│ ├── libhello
│ │ ├── CMakeLists.txt
│ │ ├── hello.c
│ │ └── hello.h
│ └── src
│ ├── CMakeLists.txt
│ └── main.c
├── cmake_fei_lib
│ ├── build
│ ├── CMakeLists.txt
│ └── libmathfei
│ ├── CMakeLists.txt
│ ├── mathfei.c
│ └── mathfei.h
首先介紹下面的cmake_fei_lib項目,這個項目主要是生成一個math動態庫
CMakeLists.txt內容如下:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(MATHFEI)
ADD_SUBDIRECTORY(libmathfei)
libmathfei下面的CMakeLists.txt內容如下:
SET(LIB_SRC mathfei.c)
ADD_DEFINITIONS("-DLIBEVEN_BUILD")
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
#dynamic library
ADD_LIBRARY(libmathfei SHARED ${LIB_SRC})
#static library
#ADD_LIBRARY(libmathfei ${LIB_SRC})
set_target_properties(libmathfei PROPERTIES OUTPUT_NAME "mathfei")
#install command is executed when "sudo make install"
install(TARGETS libmathfei DESTINATION /usr/lib)
#attention, don't create new folder in /usr/lib, just put libmathfei.so in /usr/lib directlly'
install(FILES ${CMAKE_SOURCE_DIR}/libmathfei/mathfei.h DESTINATION /usr/include/mathfei)
mathfei.c
#include <stdio.h>
#include "mathfei.h"
int mathfei_is_even(int number)
{
int value = !(number%2);
return value;
}
mathfei.h
#ifndef CMAKE_FEI_EVEN_H
#define CMAKE_FEI_EVEN_H
#ifdef _WIN32
#if LIBEVEN_BUILD
#define LIBEVEN_API __declspec(dllexport)
#else
#define LIBEVEN_API __declspec(dllimport)
#endif
#else
#define LIBEVEN_API
#endif
LIBEVEN_API int mathfei_is_even(int number);
//static library is simple as normal c code.
#endif
下面介紹上面的cmake_fei項目
最外面的CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(HELLO)
MESSAGE(STATUS "This is project binary dir" ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is project source dir" ${HELLO_SOURCE_DIR})
MESSAGE(STATUS "This is cmake binary dir" ${CMAKE_BINARY_DIR})
MESSAGE(STATUS "This is cmake source dir" ${CMAKE_SOURCE_DIR})
ADD_SUBDIRECTORY(libhello)
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(doc)
#ADD_SUBDIRECTORY(src bin)
#ADD_SUBDIRECTORY(libhello lib)
doc下面的CMakeLists.txt
install(DIRECTORY ${PROJECT_SOURCE_DIR}/doc DESTINATION /usr/apps/com.mc.cmake_fei/data FILES_MATCHING PATTERN "read*")
libhello下面的CMakeLists.txt
SET(LIB_SRC hello.c)
ADD_DEFINITIONS("-DLIBHELLO_BUILD")
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
#dynamic library
ADD_LIBRARY(libhello SHARED ${LIB_SRC})
#static library
#ADD_LIBRARY(libhello ${LIB_SRC})
set_target_properties(libhello PROPERTIES OUTPUT_NAME "hello")
#install command is executed when "sudo make install"
install(TARGETS libhello DESTINATION /usr/lib)
#attention, don't create new folder in /usr/lib, just put libhello.so in /usr/lib directlly'
install(FILES ${CMAKE_SOURCE_DIR}/libhello/hello.h DESTINATION /usr/include/hello)
hello.c
/*************************************************************************
> File Name: hello.c
> Author: ma6174
> Mail: [email protected]
> Created Time: Mon 22 Dec 2014 01:11:57 PM HKT
************************************************************************/
#include <stdio.h>
#include "hello.h"
void hello(const char *name)
{
printf("hello %s \n", name);
}
hello.h
/*************************************************************************
> File Name: hello.h
> Author: ma6174
> Mail: [email protected]
> Created Time: Mon 22 Dec 2014 01:09:54 PM HKT
************************************************************************/
#ifndef CMAKE_FEI_HELLO_H
#define CMAKE_FEI_HELLO_H
#ifdef _WIN32
#if LIBHELLO_BUILD
#define LIBHELLO_API __declspec(dllexport)
#else
#define LIBHELLO_API __declspec(dllimport)
#endif
#else
#define LIBHELLO_API
#endif
LIBHELLO_API void hello(const char *name);
//static library is simple as normal c code.
#endif
src下面的CMakeLists.txt
include_directories(${PROJECT_SOURCE_DIR}/libhello)
SET(APP_SRC main.c)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
ADD_EXECUTABLE(hello ${APP_SRC})
target_link_libraries(hello libhello)
#why libhello, mathfei?
#because libhello is generated in the same project, and the library name is libhello
#mathfei is generated in another project, and name is libmathfei, but OUTPUT_NAME is changed to mathfei
target_link_libraries(hello mathfei)
#it is executed when sudo make install
install(TARGETS hello DESTINATION /usr/bin/hello)
main.c
#include <stdio.h>
#include <mathfei/mathfei.h>
#include <hello.h>
int main()
{
hello("hello world!");
int value = mathfei_is_even(4);
if (value)
printf("%d is even number\n", 4);
else
printf("%d is odd number\n", 4);
return 0;
}
最後我們一般會創建一個build文件夾,用來專門build,這樣生成的build文件將會和source文件分開。
在build文件夾中運行cmake ../命令即可,具體命令格式可以cmake --help 查看。