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

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会自动让你连接远程环境,在调试->选项->跨平台->连接管理器中进行设置:
在这里插入图片描述
然后在–选项–连接管理器中有正常显示,即成功

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值