ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

3.linux编译C/C++程序与Windows下编译Linux C++程序

2022-01-17 19:32:07  阅读:203  来源: 互联网

标签:test1 gcc 程序 C++ 编译 aliyun main multiverse


C和C++ 编译器: gcc

GNU C Compiler 的缩写,经过十来年发展,意义变成了 GNU Compiler Collection,可同时支持 C、C++、Objective C和Java 等.

1.编译第一个C/C++程序

只编译执行一个C程序

gcc hello.c                                                                     
./a.out                                                                         
$Hello world!      

默认的a.out 并不友好,gcc 提供 -o 选项指定执行文件的文件名:

gcc -o hello.exe  hello.c       ##编译源代码,并把可执行文件命名为 hello
或gcc hello.c -o hello.exe       ##编译源代码,并把可执行文件命名为 test1.exe         
$Hello world!      

编译C++程序,我们可以直接用GCC 编译其中的g ++命令,用法同 gcc;
当然g++ 和 gcc 都可以用来编译 c 和 c++程序。
gcc 编译c++程序需要带上 -lstdc++ 指定使用c++库。

注:安装g++时如果很慢,建议:
使用vim 编辑 /etc/apt/sources.list 文件,在文件的尾部换行加入如下内容:

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

接下来执行: apt-get update 命令,再调用 sudo apt-get install g++

2.编译常用选项

选 项功 能
-c只激活预处理、编译和汇编,生成.o 目标代码文件
-S只激活预处理和编译,生成扩展名为.s的汇编代码文件
-E只激活预处理,并将结果输出至标准输出
-g为调试程序(如gdb)生成相关信息
-O等同-O1,常用的编译优化选项
-Wall打开一些很有用的警告选项,建议编译时加此选项。

注意:-c 选项在编写大型程序是必须的,多个文件的源代码首先需要编译成目标代码,再链接成执行文件。如果由多个源文件,工程做法建议采用 makefile 。

vi test1.c
vi demo.h
vi test2.c

gcc test1.c -c -o test1.o
gcc test2.c -c -o test2.o
gcc test1.o test2.o -o test1.exe
或gcc test1.c test2.c -o test1.exe

./test1.exe

test1.c

#include "demo.h"
#include <stdio.h>
        //printf("hellohelloWorld!");
int max(int a, int b){
        return a>b?a:b;
}

test2.c

#include "demo.h"
#include <stdio.h>

int main(void)
{
        int a = 10;
        int b = 9;

        printf("max:%d\n",max(a,b));
        return 0;

}

demo.h

int max(int a, int b);

3.Windows下编译Linux C++程序

(1)Ubuntu安装Samba 服务器

Samba 服务用来做Windows的文件共享的

   确认安装:   dpkg -l | grep samba
   安装:      sudo apt-get install samba samba-common
   卸载:      sudo apt-get autoremove samba

(2) Samba服务器配置

sudo vi /etc/samba/smb.conf

在文件最后添加

[Share]
comment=This is samba dir
path=/home/jessie/
writable=yes
browseable=yes

增加samba 用户密码(注意要是用户的密码)

sudo smbpasswd -a jessie

(3)启动和关闭

启动Samba服务器:    sudo service smbd start
关闭Samba服务器:   sudo service smbd stop

(4)直接在Windows 下编码

在windows系统盘文件夹中输入linux服务器的ip地址:
在这里插入图片描述
双击share
在这里插入图片描述
在这里插入图片描述
此时可在Windows和Linux中同步编程,但Windows下的代码很多不能在linux下通过

注意:Samba服务器只能用在局域网

(5)编辑器的选择

编辑器的作用:
编写程序(源代码)。

编辑器的选择:
初学者最好使用最简单的文本编辑器,不要使用集成开发环境IDE
Linux平台:vi, vim, 或gedit
Windows平台:记事本,Sublime Text, UltraEdit, notePad, notePad++, source insight

4.VS2019 开发Linux C++ 程序

第一步, 先将自己的Linux 系统设为静态IP,具体操作如下:

  1. 修改/etc/network/interfaces 地址配置文件,如下所示:
    在这里插入图片描述
    注: 查看ip命令: ip addr
    查看网关命令: ip route show

  2. 修改 /etc/resolvconf/resolv.conf.d/base 配置DNS服务器,如下所示:
    在这里插入图片描述

  3. 执行reboot 命令重启Linux系统

第二步, 对VS2019 进行设置,具体操作如下:
4. 打开vs2019,选择“工具(T)”=> “获取工具和功能(T)…”,确认“使用C++ 的Linux 开发”有勾选,本身有勾选,则忽略这一步;如果没有勾选,则选择此项,在点击右下角的修改进行安装,如下所示:
在这里插入图片描述
5. 重新打开VS2019, 创建新项目,选择Linux平台的空项目创建
在这里插入图片描述
6. 在第一次编译或调试你的项目时vs会自动让你连接远程环境,在调试->选项->跨平台->连接管理器中进行设置:
在这里插入图片描述
然后在–选项–连接管理器中有正常显示,即成功

标签:test1,gcc,程序,C++,编译,aliyun,main,multiverse
来源: https://blog.csdn.net/procurar/article/details/122536600

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有