Generating dynamic libraries and static libraries under Ubuntu _ubuntu

Source: Internet
Author: User
Pre-required work (already completed on the server) installation OpenCV, and other additional items * Test code to show when required *
can refer to http://blog.csdn.net/kuaile123/article/details/20870731 installation cmake, GCC, g++ ... Because the operating system is different, mainly contains the header file the way different, so the modified code package in the attachment Lib, copy the Lib file in the attachment to CentOS, I copy it in the desktop directory using CMakeLists.txt generated dynamic library (. So) and Static library (. A) CMakeLists.txt of writing library files
#同时生成动态库和静态库
cmake_minimum_required (VERSION 2.8)
Project (Refineoffaxis)
aux_source_directory (. DIR_SRCS)
include_directories (~/desktop/eigen)
add_library (Refineoffaxis SHARED ${dir_srcs})
SET_ Target_properties (Refineoffaxis PROPERTIES VERSION 1.0 soversion 1)
add_library (refineoffaxis_s STATIC ${dir_srcs })
set_target_properties (refineoffaxis_s PROPERTIES output_name "Refineoffaxis")
INSTALL (TARGETS Refineoffaxis refineoffaxis_s
          LIBRARY destination Lib 
          ARCHIVE destination Lib)
INSTALL (FILES RefineOffaxis.h destination Include/refineoffaxis)
1 analysis of the above CMakeLists.txt

Cmake_minimum_required (version 2.8) specifies the minimum version required for CMake

Project (Refineoffaxis) specifies the name of the item

Aux_source_directory (. Dir_srcs) aux_source_directory (dir variable) gets all the files in the specified directory and saves them to variable, including. C. C. C + +. cc. cpp. cxx. M. M. h++. H. hh. hpp. hxx. txx file, which assigns the source file name in the current directory to the variable Dir_srcs

Include_directories (~/desktop/eigen) include_directories ([after| Before] [SYSTEM] dir1 dir2 ...) Used to set the directory, these settings will be used by the compiler to find include files, because we generate dynamic libraries and static libraries in the process of using eigen, and I put the eigen in the desktop directory, so the form included as shown above, in order to beautiful, Eigen can be placed in the usr/local/include/directory

Add_library (Refineoffaxis SHARED ${dir_srcs}) add_library ([STATIC | SHARED | MODULE] [Exclude_from_all] source1 source2 ... sourcen) add a library file named, specifying shared,static. If you specify as shared, a dynamic library (. So) is generated, and if specified as STATIC, The Static library (. lib) is generated, in the example above, our husband is a dynamic library, so the argument is selected as SHARED

Set_target_properties (Refineoffaxis PROPERTIES version 1.0 soversion 1) Adds a version number to the dynamic library, version refers to the dynamic library versions, and soversion refers to the API version

Add_library (refineoffaxis_s static ${dir_srcs}) above describes the Add_library command, which is used to generate static static libraries, while requiring the generation of static libraries with the name Refineoffaxis_ s and dynamic library differentiation

Set_target_properties (refineoffaxis_s PROPERTIES output_name "Refineoffaxis") the meaning of this sentence is equivalent to our static library Refineoffaxis_ S has an alias refineoffaxis to build the beauty of the library

INSTALL (TARGETS refineoffaxis refineoffaxis_s LIBRARY destination Lib ARCHIVE destination Lib)
INSTALL (FILES refineOffaxis.h destination Include/refineoffaxis)
The above two command means to add the installation function to facilitate the installation of dynamic library static library header files to the required directory 2 CMakeLists.txt use

In the directory where CMakeLists.txt is located, the mkdir build takes an externally constructed pattern

[Haiqing@localhost build] cdbuild[haiqing@localhostbuild] cmake-dcmake_install_prefix=/usr/local ...
[Haiqing@localhost build] make[haiqing@localhostbuild] sudo make install

The final result is that so and a library are installed in the/usr/local/lib directory, RefineOffaxis.h installed in the/usr/local/include/refineoffaxis test generated dynamic library and static library 1 directory establishment

Copy the SRC folder in the attachment to the same directory as the attachment Lib
SRC has two files, one is CMakeLists.txt, one is main.cpp 2 parsing CMakeList.txt

Cmake_minimum_required (VERSION 2.8) 
PROJECT (Refineoffaxis)
find_package (OpenCV required)
add_ Executable (Refineoffaxis main.cpp)
target_link_libraries (Refineoffaxis ${opencv_libs})
#TARGET_LINK_ Libraries (Refineoffaxis Refineoffaxis)
target_link_libraries (Refineoffaxis librefineoffaxis.so)
#TARGET_ Link_libraries (Refineoffaxis librefineoffaxis.a)
include_directories (/usr/include/refineoffaxis)
Include_directories (/usr/locla/include)

The main focus is on line 6.7.8, if you comment out line 7.8, use the 6th row alone, through the LLD command can know the dynamic library of the method call, the effect and comment out 6.8 two rows of the same effect, if you want to use the static library, comment out 6.7 lines on it.
SOURCE Download Address: Https://github.com/ruyiweicas/MAKE_SO_A_UBUNTU-CentOS

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.