标签:
[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目录下,并且生成的库文件带版本号
[laolang@laolang createLibrary]$ cat CMakeLists.txt cmake_minimum_required(VERSION 3.3) project(libraryTest CXX) add_subdirectory(src) [laolang@laolang createLibrary]$
[laolang@laolang createLibrary]$ cat src/CMakeLists.txt # src CMakeLists.txt add_subdirectory(main) add_subdirectory(person) [laolang@laolang createLibrary]$
#include<iostream> #include"../../include/person.h" int main( void ){ Person * person = new Person( "小代码","15382","xiaodaima@qq.com" ); std::cout << person->toString() << std::endl; delete person; return 0; }
[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]$
/** @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_ */
/* * 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; }
[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:xiaodaima@qq.com [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]$
标签:
原文地址:http://my.oschina.net/iamhere/blog/515660