C++笔记

1 正常运行一个.cpp文件

#complie
g++ HelloWorld.cpp
#运行
./a.exe

2 静态链接库实现函数调用

2.1 把头文件Log.cpp编译成目标文件(.o)

g++ -c Log.cpp

2.2 将单个或多个目标文件合成链接库(.lib)

#单个
ar rcs Log.lib Log.o
#多个
ar rcs Log.lib Log.o Log1.o 

2.3 编译主文件链接链接库(.lib)

g++ -Wall -static HelloWorldTest.cpp -L. -lLog

2.4 示例

Log.cpp

#include "Xingyu.h"
#include 

void Log(const char* message)
{
    std::cout << message << std::endl;
}

void hello()
{
    std::cout << "what the" << std::endl;
}

Log1.cpp

#include "Xingyu.h"
#include 

void hello1()
{
    std::cout << "what the fuck" << std::endl;
}

HelloWorldTest.cpp

#include "Xingyu.h"
//#include "Log.cpp"
#include 
#include 
using namespace std;

int main()
{
    cout << "Hello World!!!!!!!test" << endl;
    // Log("Call the Log function");
    // hello();
    // hello1();
    cin.get();
    //printf("111");
    //getchar();
    //return 0;
}

3 如何Debug

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
}
#编译要debug的文件
g++ -g .\HelloWorldTest.cpp

你可能感兴趣的:(c++)