C + + compile links, makefile ideas finishing

Source: Internet
Author: User
Tags glob

Changhong Sword Own Some experience summary, specific please decide according to the actual situation.
(not finished and to be added to the details)
-A Makefile template was added in 2016-10-25
-In 2016-10-27, CMake and CMake generation NMAKE were added to organize the compiled directories under Windows to help understand the VS x86 and x64 compilation process.
-2017-6-7 added a gcc option, about library dependency order Preface

Whether on Windows or Linux we have encountered a lot of compile-link problems, if not thorough understanding of these, then makefile these are not written, that is, use vs software, if you do not have clear ideas blindly modify, it will never go out of the mire.
As for the tools for makefile, Windows has nmake,linux under the "make" and some slightly encapsulated tools Cmake,qmake. Whatever it is, the purpose of compiling the link (not the principle) can be well written.

Be careful to separate compilation and link two process areas, do not confuse compilation

The main process related to our compilation is to check whether the C + + syntax (C++11, etc.) handles some macro definitions (need to use-D) to see if the variable function is defined, in fact, with the 1th, if you introduce some additional libraries, use the-i parameter to indicate the location of the header file. This is where the compile phase is most likely to go wrong.

These errors are compile-time errors, and if you find them when you make them, be sure not to think of any missing library files. However, the general compile-phase error is very easy to solve here is not much to say.

After compiling, you typically generate an. o for each. cpp.

Reminders
There is no error in the compile phase, which does not mean that your program is not written. If you are unfamiliar with the link process, it is possible to write out a function or variable redefinition error. For example, you write a function func implementation in a. h, the result is two. This file is referenced in CPP, resulting in two. O, and these two. o files have func implementations (assumed to be in the same scope) that can cause conflicts once they have been programmed into the. O generated by the third CPP.
not just functions, non-static or very variable variables are the same. So in general. h, define static variables so that other CPP uses
"The problem is very serious, and you don't understand the principle is difficult to solve" experience

Sometimes you write a program, you already have a header file but still say a function or variable can not find, at this time may be no use using the reason. links

This is the big play, the makefile, especially in Linux for the process.
The process of compiling the main check is that you use a function has not declared it, and the link process is that you have stated that there is no way to give its implementation (even if you do not achieve, you give the. So, you have to have the implementation) if not found will be reported undefined Reference error, indicating that the definition of the function was not found (note is not a declaration, if the declaration is a compile-time problem)

Things to be aware of specify a static library general use static Library absolute path such as/PATH/LIBXXX.A, dynamic library is generally-l/path-lxxx, or use (-wl,-bstatic-lxxx link static,-wl,-bdynamic dynamic) dynamic library refers to There may be a certain order, generally it is best to first own the library, then the tripartite Library, then the system library. Sometimes the link is not but the location is correct, may be the order problem (I think the deep reason or the library name conflict), the best order is g++ ... obj ($?-L (Upper logical lib)-L (Intermediate package lib)-L (base Lib)-L (System Lib)-O $@ If you write the library path in the environment variable Ld_library_path, the format is best export ld_library_path=your_lib_path: $LD _library_path, is your own library path first. one of the benefits of this is that if you compile successfully, you are prompted with a. So file not found, which can be resolved, otherwise you-wl,-rpath,your_lib_path the compile option at compile time with Run-time lookup. Reminders

Link time two big questions, one is the link is not to the library, one is the link does not have the library which you want (for example you do not want to use the system to bring a library and so on), in fact the solution is the same. "To remember the top three points, choose" run

The general compilation link succeeds can run, the runtime said that lacks certain libraries that on the reference link-> attention matters 3rd's blackbody part, generally can solve.

After compiling, you can use the LDD command to see the executable program and the. so file to see if there is no found, even if there is no not found also look right.

If you compile a static library or a dynamic library, you can also use readelf-d to roughly see if it's what you want (you want to edit it or not). Introduction to g++ General compilation options

This or search other blog is more complete, on the one hand, I also have a lot of different, on the other hand, more than the primary and secondary points
The parameters followed by the-WL are passed to the linker ld
-WL,--as-needed ignores dynamic libraries that are not used when linking
-g-o0 is to generate a program that can be debugged (and not to be optimized by the compiler) and then debug with Gdb-tui
--start-group and--end-group's libraries in these two places will automatically resolve dependencies as described above--then discuss the static library dependency order problem at the time of GCC compilation.

when generating a static library
General Ar-cvq-o Your.a *.O on the line. "Look at other blogs."
I think the static library is a combination of several. o files, you can use Ar-t to view

generate a static library
Need to use-shared-fpic in fact, this is the two options, the latter belongs to the compile phase, preceded by the link stage, indicating the build link library. the process of compiling source code

It's usually three steps.
It's best to mkdir build, CD build, and then do your main work. /configure: This is a bash script that is primarily responsible for generating makefile "simple understanding makefile is to generate a long g++ command option" Generally this file is very normative, for example, you can use--enable-static=yes Indicates that the static library is generated, the most important one is--prefix=your_install_path, this is to say where you put the software (in the 3rd detail) make this is to perform the build above, generally in order to quickly compile with make-j. The executable program that is generated, and the library files are under the current directory. Make install this step is the installation, the second generation of things to be installed in the last place in the system. If you do not specify that it is generally under/usr or/usr/local (most of which will need to be performed in sudo), if you specify the prefix parameter in the first step, you do not need to install the root permission. if installed in your own directory, configure environment variables based on usage (In fact, you can not install, after all, the required files in the second step will already have)

Installation of the source code is usually the use of its library files and so on, their own compile time to specify the directory you install the header files and library files.
After the installation, delete the build on the line. Also make uninstall is uninstalled. Makefile

Role: First of all, do not see the makefile is too mysterious, it is mainly convenient for you to compile, you can write your own script. Be sure to realize that your purpose is to invoke the editor (such as g++) to compile your program, write makefile is also this purpose, it is ultimately to help you generate g++ this software required parameters, your compilation or g++ Ah, compile the error except your program is the problem is passed to the g++ The parameter is not right, and this parameter is wrong again because of you makefile write wrong cause .

After jumping out of Makefile's appearance, you know that generally compiling a small project is not necessary to write makefile, if you write a complex project can use CMake. A makefile example that can be used as a template

cxx=g++

cxxflags =-i/home/chj/face/program/include-i/home/chj/face/install/opencv2/include-i/home/chj/face/ Program/3rdparty
ldflags =-l/home/chj/face/install/opencv2/lib-lopencv_core-lopencv_highgui-lopencv_ features2d
staticlib =/home/chj/face/program/build/libliblinear.a/home/chj/face/program/build/ LIBLIB3000FPS.A   

# Easy to make
all:main

TARGET = Main

# Find all the required cpp
CHJ_SRCS: = $ (Wildcard ... /SRC/*.CPP) $ (wildcard. /src/lbf/*.cpp)
# Replace the. cpp found above with. O
chj_objs = $ (Patsubst%cpp,%o, $ (CHJ_SRCS)) 

# $@ is preceded by a colon; $< is the first after the colon; $^ after colon all
# compile
%.o:%.cpp  
    # $ (Warning $@) 
    $ (CXX) $ (cxxflags)-C $<-o $@ 

# link (I have a file here with int mai N (), direct build executable)   
$ (TARGET): $ (CHJ_OBJS)
    # Order is important, $ (staticlib) not in front of
    $ (CXX) $ (ldflags)-O $@ $ ( Staticlib) 
CMake

CMake when I use Windows, I think he is really convenient. Many libraries provide cmakelist and then directly use the CMake program under win to generate VS projects, and then open with vs. commonly used Syntax cmake_minimum_required (version 3.5) This generally has to be determined by your CMake minimum version of Project (XXX) to give your item a name that may be used as the default build name XXX.exe Set (cmake_cxx_flags_release "${env{cxxflags}-o3-wall") This set command is the meaning of the assignment, and the attached value may be either a system variable or a variable of its own. Some of the commonly used variables are

The following variables generally have a default value
Cmake_c_compiler Cmake_cxx_compiler Cmake_cxx_flags_debug
Cxxflags This is the compile-time option.
Cmake_source_dir This is your Cmakelist.txt directory include_directories () This is the directory where you add header files, a directory where you can write multiple directories link_directories add a library file add_ Executable (Face_align ${source_files}) This is the generated program you added, followed by the. cpp file Target_link_libraries (face_align ${opencv_libs} Dlib.lib) Add a library that generates additional requirements for your program, and if you write only the name of the library, the library's directory must give the syntax in the command line in Link_directories

As an example
Cmake-g "NMake makefiles"-dcmake_build_type=release.
Generating NMAKE makefile using the release mode example: compilation library

Suppose your. CPP is in the same directory

File (GLOB hdrs_base "*.h") file (
GLOB srcs_base "*.cpp") file (GLOB
HDRs ${hdrs_base}  ) file
(GLOB srcs< C4/>${srcs_base})

add_library (${project_name} ${srcs} ${hdrs})  # should be determined by adding a SHARED or STATIC to what library to build

Below goes the section on links under Windows. The compilation of compiling directory under Win7

Several files related to compiling connections under Windows for compiler cl.exe linker link.exe Resource Compiler Rc.exe Here's a more complete introduction

CL and link are generally in C:\Program Files (x86) \microsoft Visual Studio 14.0\vc\bin\[amd64| ...]
RC General in C:\Program Files (x86) \ Windows Kits\8.1\bin\[x64]
Note: The above brackets indicate the directory of the x64 at that time, so be sure to choose the appropriate compiler if you use CMake to generate the NMAKE file. NMAKE (not IDE compiled under win)

Do not want to detail the grammar, has not been directly used, generally direct use of win under the CMake and then with VS, or use CMake to generate NMAKE required files, or install MinGW and then directly make.

If you want to specify its directory with NMAKE, you can use the corresponding VS version of NMAKE. For example, if you want to use vs2015 64-bit compilation, run it on the command line first.
"C:\Program Files (x86) \microsoft Visual Studio 14.0\vc\vcvarsall.bat" x64
If you write in the batch script, just precede the start with the configuration and experience of Visual Studio compile-time

Generally you write projects will always rely on a lot of libraries, such as OPENCV, so your project needs to specify at least OpenCV header files in the directory , the directory where the library files , each time this designation is cumbersome, You might as well write yourself a. Props and then join in the property manager.
Here is a template for me to modify the corresponding location (the directory is best with absolute path) ExecutablePath this can not.

<?xml version= "1.0" encoding= "Utf-8"?> <project toolsversion= "4.0" xmlns= "http://schemas.microsoft.com/"
  developer/msbuild/2003 "> <importgroup label=" propertysheets "/> <propertygroup Label=" UserMacros "/> <PropertyGroup> <executablepath>opencv\3.1.0\opencv\build\x64\vc12\bin;$ (ExecutablePath) </ executablepath> </PropertyGroup> <PropertyGroup> <includepath>opencv\3.1.0\opencv\build\incl ude;$ (Includepath) </IncludePath> </PropertyGroup> <PropertyGroup> <librarypath>opencv\3.1 .0\opencv\build\x64\vc12\lib;$ (LibraryPath) </LibraryPath> <librarywpath>$ (Librarywpath) </ librarywpath> </PropertyGroup> <ItemDefinitionGroup> <Link> <additionaldependencies& gt;opencv_world310d.lib;% ( additionaldependencies) </AdditionalDependencies> </Link> </ItemDefinitionGroup> <itemgroup/&
Gt </Project>

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.