Day1 notes of high tech small training (C + +)

preface

  1. IDE tools

    • VSCode
      • Syntax detection: include header file + compile built-in syntax
      • Add header file detection: Ctrl + Shift + P - > C + + configuration editing, add the directory where the header file is located;
  2. Control terminal

    • command
    • environment variable
      • PATH: echo %PATH%
      • set instruction shows all environment variables
      • PATH: the PATH where the window performs the program's search; each PATH is separated by a semicolon
      • With visual settings
  3. The setting of Visual Studio development environment

    • The script is vcvars64.bat
    • Set the directory of vcvars64.bat to the PATH environment variable;
      • PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build
    • To set environment variables visually, it is necessary to restart the terminal;
  4. development tool

    • cl compiler
    • link connector (PE format)
    • lib library archiving tool (static library: archiving of target files)
    • dumpbin PE and target file format analysis tool

Engineering organization and compiler

  1. compiler

    • cl source code compile / link to executable
      • /EHsc
      • /MD
      • /utf-8
        • /source-charset:utf-8
        • /execution-charset:utf-8
      • Header directory
  2. explain:

    • The first time you output, Chinese characters may be garbled
      1. Use the chcp command to change the code page of the terminal. Code page of utf-8 = 65001
        • chcp 65001
      2. Set the encoding directly in the region / language (the whole system is set to utf-8)
        • opencv does not recognize Chinese Directory; (system setting code)
  3. Linker

    • link
      • cl is responsible for compiling: checking syntax and generating target files
      • link is responsible for linking: responsible for generating PE format files, requiring information dynamic library information
    • link option s target files
      • /out
      • /MACHINE:X64
      • Library Directory of third party libraries
      • Third party's library name

Static library

  • Premise: lib/dll two files are explained clearly;
    • lib: static library
    • dll: dynamic library

Implement a library function

  • gk_math.h
#ifndef GK_MATH_H
#define GK_MATH_H
extern int gk_add(int, int);
#endif

  • gk_maath.cpp
#include "gk_math.h"

int gk_add(int p1, int p2){
    return p1 + p2;
}

Compile to static library

@rem Compilation of static library
@rem compile
@cl /c /EHsc /MD /nologo /source-charset:utf-8  /execution-charset:utf-8 /Fo:gkmath.obj  gk_math.cpp
@rem Static library links
lib /MACHINE:X64 /nologo /OUT:gkmath.lib  gkmath.obj

Code organization

  • When using shell script or bat to process scripts, it is more troublesome that multiple operations need to be written into multiple bat files;

  • Project organization mode:

    • currency
      • Makefile (common, engineering and system)
      • CMake
      • QMake
    • individualization:
      • Visual Studio
      • Qt Creator
      • QMake
      • Eclipse C++
      • C++ Builder
  • Syntax of a Makefile script: Makefile

    1. Defining variables
    2. Task
      • Dependency (task depends on another task)
    3. instructions
  • The Makfile example (there must be no space after the line break '\', otherwise an error will be reported. )

CL_FLAGS   = /c \
		     /EHsc \
		     /MD  \
		     /nologo \
		     /source-charset:utf-8 \
		     /execution-charset:utf-8

LINK_FLAGS = /MACHINE:X64 \
		     /nologo

OBJS       = gkmath.obj
SOURCES    = gk_math.cpp
TARGETS    = gkmath.lib

main:$(TARGETS) main.cpp
	@ cl /nologo /MD /Fe:main.exe main.cpp  $(TARGETS) 

$(TARGETS):$(OBJS)
	@lib $(LINK_FLAGS) /OUT:$(TARGETS)  $(OBJS)

$(OBJS): gk_math.h  gk_math.cpp
	@cl  $(CL_FLAGS) /Fo:$(OBJS)  $(SOURCES)


clean:
	@del *.exe *.obj *.lib 2>/Nul

Using static libraries

  1. Use static libraries when linking
#include <stdio.h>
#include "gk_math.h"
int main(int argc, char **argv, char **arge){
    printf("C++Programming! Static library call result:%d\n", gk_add(45, 55));
    return 0;
}

  • Compile script
CL_FLAGS   = /c \
		     /EHsc \
		     /MD  \
		     /nologo \
		     /source-charset:utf-8 \
		     /execution-charset:utf-8

LINK_FLAGS = /MACHINE:X64 \
		     /nologo

OBJS       = gkmath.obj
SOURCES    = gk_math.cpp
TARGETS    = gkmath.lib

main:$(TARGETS) main.cpp
	@ cl /nologo /MD /Fe:main.exe main.cpp  $(TARGETS) 

$(TARGETS):$(OBJS)
	@lib $(LINK_FLAGS) /OUT:$(TARGETS)  $(OBJS)

$(OBJS): gk_math.h  gk_math.cpp
	@cl  $(CL_FLAGS) /Fo:$(OBJS)  $(SOURCES)


clean:
	@del *.exe *.obj *.lib 2>/Nul
  1. Using static libraries in code
#include <stdio.h>
#include "gk_math.h"

#pragma comment(lib, "gkmath.lib")

int main(int argc, char **argv, char **arge){
    printf("C++Programming! Static library call result:%d\n", gk_add(45, 55));
    return 0;
}
// cl /nologo /MD /Fe:main.exe main_lib.cpp

  • Compile command:
    • cl /nologo /MD /Fe:main.exe main_lib.cpp
  • review
    1. development tool
      1. cl compiler (mac: clang/g + +, posix: gnu g + +, hp: acc: intel:cc, sun:cc)

        • Default is to call link default link
          • /Link options are directly included after link
      2. link connector (posix: ld)

        • link step default auto call in many compilers
      3. lib(ar)

        • Static library
      4. dumpbin(nm)

        • Analyze target file and PE executive file
      5. nmake(make)

        • nmake task
        • nmake task -f makefile file
    2. vcvars64.bat / vcvars32.bat
      • Space escape: "C:\Program Files (x86)\Microsoft Visual Studio"
    3. Syntax of makefile
      • Mission objective: dependency
        • Instruction (start with tab)
      • Pseudo task target:
        • file does not exist

Dynamic library

Implementation code

  1. gkmath.h file
#ifndef GK_MATH_H
#define GK_MATH_H
extern int gk_add(int, int);
#endif

  1. gkmath.cpp file
#include "gkmath.h"

int gk_add(int p1, int p2){
    return p1 + p2;
}

Compiling dynamic libraries

  • Prepare: link option

    • /DLL: no main entry required
    • /IMPLIB: Specifies that the imported symbols will be generated when the link is specified and stored in the way of lib static library;
    • /EXPORT: specify which functions can be called by others = / DEF: DEF EXPORT function description file
    • /MACHINE: specify CPU structure X64/X86/ARM/ARM64/EBC
    • /OUT: specify output file name, dll output name
  • Compile script

# Compile option to set a variable
CL_ARGS=/EHsc /MD /source-charset:utf-8 /execution-charset:utf-8 /nologo
# Link option to set a variable
LINK_ARGS=/MACHINE:X64 /NOLOGO /DLL

# File set as variable
SOURCES  	 = gkmath.cpp
OBJS	     = gkmath.obj
OUTLIBS      = gkmath.lib
OUTDLLS      = gkmath.dll

# Target instruction implementation
$(OUTDLLS):$(SOURCES)
	@cl /c  $(CL_ARGS) /Fo:$(OBJS)  gkmath.cpp
	@link /MACHINE:X64 /NOLOGO /DLL /OUT:$(OUTDLLS)  /IMPLIB:$(OUTLIBS) /EXPORT:gk_add  $(OBJS)
clean:
	@del *.obj *.lib *.dll *.ilk *.exe *.exp 2>/Nul

Dynamic library call mode 1

  • Use dll directly to call functions (lib doesn't need at all, only dll is needed) [not recommended]

  • Preparation techniques:

    1. Hmodule = loadlibraria (lpcstr dllfilename): load dynamic library into memory
    2. FARPROC = GetProcAddress(HMODULE hModule, LPCSTR functioname)
    3. Function type conversion
    4. call
    5. Freeing dll space: BOOL FreeLibraray(HMODULE)
  • code implementation

call_manual_dll.cpp

#include <stdio.h>
#include <windows.h>

// typedef int(*type_f)(int,int);

int main(int argc, const char**argv){
    // Load dll module
    HMODULE h = LoadLibraryA("gkmath.dll");
    if (h == NULL){
        printf("Load failed!\n");
        return -1;
    }
    printf("Loading succeeded!");
    // Find function
    FARPROC f = GetProcAddress(h, "gk_add");  // ?gk_add@@YAHHH@Z
    printf("%p\n", f);
    // Type conversion
    // type_f myfunc = (type_f)f;
    int (*myfunc)(int, int) = (int(*)(int, int))f;
    // call
    printf("result:%d\n", myfunc(45,55));
    // Release module
    FreeLibrary(h);
}

build.bat

@cl /c /utf-8 /nologo /MD  /Fo:call_manual_dll.obj  call_manual_dll.cpp
@link /NOLOGO /OUT:main.exe /DYNAMICBASE  Kernel32.lib call_manual_dll.obj
  • compile

  • /I: specify the path of the header file

Dynamic library call mode 2

  • Call function when compiling (dll is not needed at all, only lib is needed, but dll and lib are not needed when running)
call_auto_dll.cpp

#include <stdio.h>
#include "gkmath.h"
#pragma comment(lib, "gkmath.lib ") / / not recommended
int main(int argc, const char*argv[]){
    printf("Call results: %d\n", gk_add(55,55));
    
    return 0;
}


Makefile

# Compile option to set a variable
CL_ARGS=/EHsc /MD /source-charset:utf-8 /execution-charset:utf-8 /nologo
# Link option to set a variable
LINK_ARGS=/MACHINE:X64 /NOLOGO /DLL

# File set as variable
SOURCES  	 = gkmath.cpp
OBJS	     = gkmath.obj
OUTLIBS      = gkmath.lib
OUTDLLS      = gkmath.dll

# Target instruction implementation
$(OUTDLLS):$(SOURCES)
	@cl /c  $(CL_ARGS) /Fo:$(OBJS)  gkmath.cpp
	@link /MACHINE:X64 /NOLOGO /DLL /OUT:$(OUTDLLS)  /IMPLIB:$(OUTLIBS) /EXPORT:gk_add  $(OBJS)
clean:
	@del *.obj *.lib *.dll *.ilk *.exe *.exp 2>/Nul

main:call_auto_dll.cpp
	@cl /c $(CL_ARGS) /Fo:main.obj call_auto_dll.cpp
	@link /OUT:main.exe  /DYNAMICBASE gkmath.lib  main.obj

main1:call_auto_dll.cpp
	@cl /c $(CL_ARGS) /Fo:main1.obj call_auto_dll.cpp
	@link /OUT:main1.exe  main1.obj

Qt compilation environment settings

  1. Key points:

    1. GUI (Qt application + QtWidgets (QDialog))

    Add path after "${workspaceFolder} /"
    For example, "C:/Qt/Qt-5.14.0/include /"

  2. Qt procedure

#include <iostream>

// Qt GUI modular: QtWidgets
// Qt Bottom module: QtCore
// Qt Drawing module of graphics QtGui
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>

int main(int argc, char **argv){
    // 1. structure Qt application:QApplication
    QApplication app(argc, argv);
    // 2. Form creation QDialog
    QDialog dlg;
    // Properties of the form (function pair getter/setter)
    dlg.setWindowTitle("Qt development");
    dlg.resize(640, 480);
    dlg.move(200,200);
    dlg.show();
    // 3. Message loop processingint QApplication.exec()
    int status = app.exec();  // block Function (message loop)
    // 4. Exit the program and return the status code to the system0-255(-1=255)
    return status;
}

// dll The path must be set to PATH environment variable
  1. Qt compilation (include and lib paths)
INCLUDES = /I "C:\Qt\Qt-5.14.0\include"

LIBS     = /LIBPATH:"C:\Qt\Qt-5.14.0\lib" \
           /DYNAMICBASE  \
	         "Qt5Widgetsd.lib"  \
		     "Qt5Guid.lib" \
		     "Qt5Cored.lib"
CL_ARGS  = /EHsc \
           /MDd \
		   /source-charset:utf-8 \
		   /execution-charset:utf-8 \
		   /nologo

LINK_ARGS = /MACHINE:X64  /NOLOGO 

main:qmain.cpp
	@cl /c $(CL_ARGS) /Fo:main.obj  $(INCLUDES) qmain.cpp
	@link $(LINK_ARGS) $(LIBS) /OUT:main.exe  main.obj

clean:
	@del *.exe *.obj *.exp 2>/Nul

OpenCV's environment

  1. cmake configure

  2. generate

  3. VS2019 open: BUILD_ALL / INSTAL

task

  1. Write Qt program, compile and link it into execution file, and it can be executed successfully;
    • Dynamic use
    • Compiler / connector

Perception

1. Make, change to a professional programmer, I haven't learned C + + before, how about being completely ignorant Then I found that sometimes the installation program is also a big problem for programmers, but after digestion, these contents become a little acceptable. Just keep going.

Tags: Qt Makefile OpenCV cmake

Posted on Thu, 25 Jun 2020 04:00:29 -0400 by jaimitoc30