【C/C++ 2】Clion配置與運行C語言

【C/C++ 1】Clion配置與運行C語言
【C/C++ 2】Clion配置與運行C語言

一、C++調用外部文件中的函數(clion)

方法一、 在當前項目中建一新項目,把下列文件添中到項目中
     主函數map1.cpp,其中添加 #include “map7.h”,

方法二: 在主函數map1.cpp中直接中添加 #include “add.cpp”,#include " sub.cpp",把這三個文件放在同一目錄下。參考鏈接:方法二

我這裏示例方法一:
1.
在這裏插入圖片描述
頭文件map7.h 聲明 map7.ccp中函數int test_map7();

#ifndef MAP_MAP7_H
#define MAP_MAP7_H



int test_map7();

#endif //MAP_MAP7_H

map7.ccp 文件


```cpp

#include "map7.h"
#include <iostream>

using namespace std;

int test_map7(){
    int var1;
    char var2[10];
    cout << "var1變量的地址" << &var1 <<endl;
    cout << "var2變量的地址" << &var2 <<endl;

    int var =20;//實際變量的聲明
    int *ip; //指針變量的聲明

    ip=&var; //在指針變量中存儲var的地址
    

    cout<< "value of var variable"<<var <<endl;
    //輸出在指針變量中存儲的地址
    cout << "var變量的地址" << &var <<endl;
    cout<< "address stored in ip variable:"<< ip<<endl;
    // 訪問指針中地址de值
    cout <<"value of *ip variable:" << *ip <<endl;
}

在map1.cpp中引用


#include<iostream>
#include "map7.h"

using namespace std;

int main() {
    cout << "type: \t\t" << "************size**************" << endl;
   
    test_map7();
    return 0;
}

在clion中注意要修改CMakeLists.txt,
add_executable(map map1.cpp) 中增加引用的函數add_executable(map map1.cpp map7.cpp)

cmake_minimum_required(VERSION 3.14)
project(map)


set(CMAKE_CXX_STANDARD 14)
add_executable(map   map1.cpp map7.cpp)

Terminal vim c++程序

vim編寫C、C++程序過程(以hello world爲例):
1、vim hello.c/hello.cpp。
創建hello.c/hello.cpp文件,並進入vim界面;
2、鍵入i,進入輸入模式。
3、編寫hello.c/hello.cpp程序。
4、先按Esc鍵,退出輸入模式,:wq (按Shift+:鍵,進入命令模式。輸入wq),保存並退出。
在這裏插入圖片描述
5、編譯hello.c/hello.cpp。
5.1、對於C程序:用gcc hello.c便可編譯;
5.2、對於C++程序:用g++ hello.cpp可編譯。編譯後,均生成a.out的可執行文件;
在這裏插入圖片描述
6、鍵入指令"./a.out",運行程序。
在這裏插入圖片描述
7、在Terminal上顯示"hello world"即成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章