cmakeがダイナミックライブラリと静的ライブラリを同時に生成する方法

6297 ワード

マイディレクトリ構造

[laolang@laolang createLibrary]$ tree
.
├── bin
├── build
├── CMakeLists.txt
├── include
│   └── person.h
├── lib
└── src
    ├── CMakeLists.txt
    ├── main
    │   ├── CMakeLists.txt
    │   └── main.cpp
    └── person
        ├── CMakeLists.txt
        └── person.cpp

7 directories, 7 files
[laolang@laolang createLibrary]$

最後に生成されたライブラリファイルはlibディレクトリの下に配置され、生成されたライブラリファイルにはバージョン番号が付きます.

最上位CMakeLists.txt

[laolang@laolang createLibrary]$ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.3)
project(libraryTest CXX)

add_subdirectory(src)
[laolang@laolang createLibrary]$

src/CMakeLists.txt

[laolang@laolang createLibrary]$ cat src/CMakeLists.txt 
# src CMakeLists.txt
add_subdirectory(main)
add_subdirectory(person)
[laolang@laolang createLibrary]$

src/main/main.cpp

#include<iostream>

#include"../../include/person.h"
int main( void ){
  Person * person = new Person( " ","15382","[email protected]" );
  std::cout << person->toString() << std::endl;
  delete person;
  return 0;
}

src/main/CMakeLists.txt

[laolang@laolang createLibrary]$ cat src/main/CMakeLists.txt 
# contacts CMakeLists.txt
aux_source_directory(. srcs)


include_directories(${PROJECT_SOURCE_DIR}/include  )

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

add_executable( librarytest ${srcs})

link_directories(${PROJECT_SOURCE_DIR}/lib)

target_link_libraries(librarytest
  person
  )
[laolang@laolang createLibrary]$

include/person.h

/** @file person.h
 *  
 * 
 */
#ifndef INCLUDE_PERSON_H_
#define INCLUDE_PERSON_H_

#include<string>

using std::string;


/**
 *  
 */
class Person {
public:

	/**
	 *  
	 */
	Person();
	/**  
	 * 
	 * @param name  
	 * @param phone  
	 * @param email  
	 */
	Person(string name, string phone, string email);
	~Person();

	/**  
	 * 
	 * @return  std::string 
	 */
	string toString();

	const string& getEmail() const;
	void setEmail(const string& email);
	const string& getName() const;
	void setName(const string& name);
	const string& getPhone() const;
	void setPhone(const string& phone);

private:
	/**<    */
	string name;
	/**<    */
	string phone;
	/**<    */
	string email;
};

#endif /* INCLUDE_PERSON_H_ */

src/person/person.cpp

/*
 * person.cpp
 *
 *  Created on: 2015 9 30 
 *      Author: laolang
 */

#include"../../include/person.h"

Person::Person() {
}

Person::Person(string name, string phone, string email) {
	this->name = name;
	this->phone = phone;
	this->email = email;
}

Person::~Person() {
}

string Person::toString() {
	string str = "Person:";
	str += " name:";
	str += this->getName();
	str += "\tphone:";
	str += this->getPhone();
	str += "\temail:";
	str += this->getEmail();
	return str;
}

const string& Person::getEmail() const {
	return email;
}

void Person::setEmail(const string& email) {
	this->email = email;
}

const string& Person::getName() const {
	return name;
}

void Person::setName(const string& name) {
	this->name = name;
}

const string& Person::getPhone() const {
	return phone;
}

void Person::setPhone(const string& phone) {
	this->phone = phone;
}

src/person/CMakeLists.txt

[laolang@laolang createLibrary]$ cat src/person/CMakeLists.txt 
aux_source_directory(. srcs)

include_directories(${PROJECT_SOURCE_DIR}/include )

set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#  / 
#  : 
# add_library(person SHARED ${srcs})

#  
add_library(person SHARED ${srcs})
#  
add_library(person_static STATIC ${srcs})

#  
set_target_properties(person_static PROPERTIES OUTPUT_NAME "person")
#  
set_target_properties(person PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(person_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)

#  
# VERSION  
# SOVERSION API 
set_target_properties(person PROPERTIES VERSION 1.0 SOVERSION 1)

[laolang@laolang createLibrary]$

実行効果:

[laolang@laolang createLibrary]$ ls -l
  20
drwxr-xr-x 2 laolang users 4096 10  12 02:19 bin
-rw-r--r-- 1 laolang users   84 10  12 02:17 CMakeLists.txt
drwxr-xr-x 2 laolang users 4096 10  12 02:09 include
drwxr-xr-x 2 laolang users 4096 10  12 02:19 lib
drwxr-xr-x 4 laolang users 4096 10  12 02:18 src
[laolang@laolang createLibrary]$ mkdir build
[laolang@laolang createLibrary]$ cd build
[laolang@laolang build]$ cmake ..
-- The CXX compiler identification is GNU 5.2.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/laolang/code/cmake/createLibrary/build
[laolang@laolang build]$ make
Scanning dependencies of target person
[ 16%] Building CXX object src/person/CMakeFiles/person.dir/person.cpp.o
[ 33%] Linking CXX shared library ../../../lib/libperson.so
[ 33%] Built target person
Scanning dependencies of target librarytest
[ 50%] Building CXX object src/main/CMakeFiles/librarytest.dir/main.cpp.o
[ 66%] Linking CXX executable ../../../bin/librarytest
[ 66%] Built target librarytest
Scanning dependencies of target person_static
[ 83%] Building CXX object src/person/CMakeFiles/person_static.dir/person.cpp.o
[100%] Linking CXX static library ../../../lib/libperson.a
[100%] Built target person_static
[laolang@laolang build]$ ../bin/librarytest 
Person: name: 	phone:15382	email:[email protected]
[laolang@laolang build]$ cd ..
[laolang@laolang createLibrary]$ ls -l lib/
  20
-rw-r--r-- 1 laolang users  5608 10  12 02:25 libperson.a
lrwxrwxrwx 1 laolang users    14 10  12 02:25 libperson.so -> libperson.so.1
lrwxrwxrwx 1 laolang users    16 10  12 02:25 libperson.so.1 -> libperson.so.1.0
-rwxr-xr-x 1 laolang users 10688 10  12 02:25 libperson.so.1.0
[laolang@laolang createLibrary]$