CMake simultaneous generation of dynamic libraries and static libraries

Source: Internet
Author: User

My directory structure


[Email protected] createlibrary]$ tree.├──bin├──build├──cmakelists.txt├──include│└──person.h├──lib└──src    ├─ ─cmakelists.txt    ├──main    │├──cmakelists.txt    │└──main.cpp    └──person        ├──cmakelists.txt        └─ ─PERSON.CPP7 directories, 7 Files[[email protected] createlibrary]$

The last generated library file will be placed in the Lib directory, and the resulting library file with the version number

Top Floor CMakeLists.txt


[Laol[email protected] createlibrary]$ cat CMakeLists.txt cmake_minimum_required (VERSION 3.3) project (Librarytest CXX) Add_subdirectory (SRC) [[email protected] createlibrary]$
Src/cmakelists.txt



[email protected] createlibrary]$ Cat src/cmakelists.txt # src cmakelists.txtadd_subdirectory (main) add_subdirectory ( person) [[email protected] createlibrary]$
Src/main/main.cpp



#include <iostream> #include ". /.. /include/person.h "int main (void) {person  * person = new Person (" Small Code "," 15382 "," [email protected] ");  Std::cout << person->tostring () << Std::endl;  Delete person;  return 0;}




Src/main/cmakelists.txt


[email protected] createlibrary]$ Cat Src/main/cmakelists.txt # Contacts Cmakelists.txtaux_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  ) [Email protected] createlibrary]$




Include/person.h


/** @file person.h * Contact Definition * */  #ifndef include_person_h_#define include_person_h_#include<string>using std:: string;/** * Contact definition */class Person {public:/** * default constructor method */person ();/** Custom Construction method *  * @param name name * @param phone * @param email */person (string name, string phone, string email), ~person ();/** Output Contact information *  * @return A std::st containing contact information Ring 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);p rivate:/**< name  */string name;/**< phone  */string phone;/**< email  */string email;}; #endif/* Include_person_h_ */




Src/person/person.cpp


/* * person.cpp * *  Created on:2015 September 30 *      Author:laolang */#include ". /.. /include/person.h "Person::P Erson () {}person::P erson (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 + = "\ t Phone: "; 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


[email protected] createlibrary]$ cat Src/person/cmakelists.txt aux_source_directory (. SRCs) Include_directories (${ Project_source_dir}/include) Set (Library_output_path ${project_source_dir}/lib) # Previous method of adding a dynamic library/static Library # Disadvantage: The name of the dynamic library and the static library cannot be the same as # add_library (person shared ${srcs}) # Generate dynamic library target add_library (person shared ${srcs}) # generate static library destination Add_library ( Person_static static ${srcs}) # Specifies the output name of the static library Set_target_properties (Person_static properties Output_name "Person") # Make both the dynamic library and the static library exist Set_target_properties (person Properties Clean_direct_output 1) set_target_properties (person_static PROPERTIES clean_direct_output 1) # Specify dynamic Library version # version dynamic library versions # Soversion API version set_target_properties (person properties VERSION 1.0 soversion 1) [[email protected] createlibrary]$
Operating effect:



[[email protected] createlibrary]$ ls-l total dosage 20drwxr-xr-x 2 Laolang users 4096 October 02:19 bin-rw-r--r--1 Laolang US ERS 84 October 02:17 cmakelists.txtdrwxr-xr-x 2 Laolang users 4096 October 02:09 includedrwxr-xr-x 2 Laolang users 4096 10 Month 02:19 libdrwxr-xr-x 4 Laolang users 4096 October 02:18 src[[email protected] createlibrary]$ mkdir BUILD[[EMAIL&N Bsp;protected] createlibrary]$ CD build[[email protected] build]$ cmake. --The CXX compiler identification is a GNU 5.2.0--check for working CXX compiler:/usr/bin/c++--Check for working CXX com Piler:/usr/bin/c++--works--detecting CXX compiler ABI info--detecting CXX compiler ABI info-done--detecting CXX Co Mpile features--detecting CXX compile features-done--configuring done--generating done--Build files have been Writte N to:/home/laolang/code/cmake/createlibrary/build[[email protected] build]$ makescanning 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 personscanning 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 librarytestscanning 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[[email protected] build]$. /bin/librarytest person:name: Small Code phone:15382email:[email protected][[email protected] build]$ CD. [[email protected] createlibrary]$ ls-l lib/Total usage 20-rw-r--r--1 Laolang users 5608 October 02:25 LIBPERSON.ALRWXRWXRW X 1 Laolang users 14 October 02:25 libperson.so-libperson.so.1lrwxrwxrwx 1 Laolang users 16 October 02:25 Libper Son.so.1-Libperson.so.1.0-rwxr-xr-x 1 Laolang users 10688 October 02:25 libperson.so.1.0[[email p rotected] createlibrary]$ 





CMake simultaneous generation of dynamic libraries and static libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.