C语言 通过 SSH 使用 Eclipse 处理远程项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4216822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:06:35  来源:igfitidea点击:

Work on a remote project with Eclipse via SSH

ceclipsessheclipse-cdtremote-debugging

提问by Kos

I have the following boxes:

我有以下盒子:

  1. A Windows box with Eclipse CDT,
  2. A Linux box, accessible for me only via SSH.
  1. 带有 Eclipse CDT 的 Windows 框,
  2. 一个 Linux 机器,我只能通过 SSH 访问。

Both the compiler and the hardware required to build and run my project is only on machine B.

构建和运行我的项目所需的编译器和硬件都只在机器 B 上。

I'd like to work "transparently" from a Windows box on that project using Eclipse CDT and be able to build, run and debug the project remotely from within the IDE.

我想使用 Eclipse CDT 从该项目的 Windows 框中“透明地”工作,并且能够从 IDE 中远程构建、运行和调试项目。

How do I set up that:

我该如何设置:

  • The building will work? Any simpler solutions than writing a local makefile which would rsyncthe project and then call a remote makefile to initiate the actual build? Does Eclipse managed build have a feature for that?
  • The debugging will work?
  • Preferably - the Eclipse CDT code indexing will work? Do I have to copy all required header files from machine B to machine A and add them to include path manually?
  • 大楼能用吗?有没有比编写本地生成文件更简单的解决方案,rsync该项目将调用远程生成文件以启动实际构建?Eclipse 托管构建是否具有此功能?
  • 调试会起作用吗?
  • 最好 - Eclipse CDT 代码索引会起作用吗?我是否必须将所有必需的头文件从机器 B 复制到机器 A 并手动添加它们以包含路径?

回答by Aaron Digulla

Try the Remote System Explorer(RSE). It's a set of plug-ins to do exactly what you want.

尝试远程系统资源管理器(RSE)。它是一组插件,可以完全满足您的需求。

RSE may already be included in your current Eclipse installation. To check in Eclipse Indigo go to Window> Open Perspective> Other...and choose Remote System Explorerfrom the Open Perspectivedialog to open the RSE perspective.

RSE 可能已经包含在您当前的 Eclipse 安装中。要检查 Eclipse Indigo,请转至Window> Open Perspective> Other...并从Open Perspective对话框中选择Remote System Explorer以打开 RSE 透视图。

To create an SSH remote project from the RSE perspective in Eclipse:

在 Eclipse 中从 RSE 角度创建 SSH 远程项目:

  1. Define a new connection and choose SSH Only from the Select Remote System Type screen in the New Connection dialog.
  2. Enter the connection information then choose Finish.
  3. Connect to the new host. (Assumes SSH keys are already setup.)
  4. Once connected, drill down into the host's Sftp Files, choose a folder and select Create Remote Projectfrom the item's context menu. (Wait as the remote project is created.)
  1. 定义一个新连接并从 New Connection 对话框的 Select Remote System Type 屏幕中选择 SSH Only。
  2. 输入连接信息,然后选择完成。
  3. 连接到新主机。(假设已经设置了 SSH 密钥。)
  4. 连接后,深入到主机的Sftp Files,选择一个文件夹并从项目的上下文菜单中选择Create Remote Project。(等待创建远程项目。)

If done correctly, there should now be a new remote project accessible from the Project Explorer and other perspectives within eclipse. With the SSH connection set-up correctly passwords can be made an optional part of the normal SSH authentication process. A remote project with Eclipse via SSH is now created.

如果操作正确,现在应该可以从项目资源管理器和 eclipse 中的其他视角访问一个新的远程项目。通过正确设置 SSH 连接,密码可以成为正常 SSH 身份验证过程的可选部分。现在创建了一个通过 SSH 使用 Eclipse 的远程项目。

回答by Lagerbaer

The very simplest way would be to run Eclipse CDT on the Linux Box and use either X11-Forwarding or remote desktop software such as VNC.

最简单的方法是在 Linux Box 上运行 Eclipse CDT 并使用 X11-Forwarding 或远程桌面软件,例如 VNC。

This, of course, is only possible when you Eclipse is present on the Linux box and your network connection to the box is sufficiently fast.

当然,这只有在 Linux 机器上存在 Eclipse 并且与该机器的网络连接足够快时才有可能。

The advantage is that, due to everything being local, you won't have synchronization issues, and you don't get any awkward cross-platform issues.

优点是,由于一切都是本地的,您不会有同步问题,也不会遇到任何尴尬的跨平台问题。

If you have no eclipse on the box, you could thinking of sharing your linux working directory via SMB (or SSHFS) and access it from your windows machine, but that would require quite some setup.

如果您没有安装 Eclipse,您可以考虑通过 SMB(或 SSHFS)共享您的 linux 工作目录并从您的 Windows 机器访问它,但这需要相当多的设置。

Both would be better than having two copies, especially when it's cross-platform.

两者都比拥有两个副本要好,尤其是在跨平台时。

回答by timB33

I'm in the same spot myself (or was), FWIW I ended up checking out to a samba share on the Linux host and editing that share locally on the Windows machine with notepad++, then I compiled on the Linux box via PuTTY. (We weren't allowed to update the ten y/o versions of the editors on the Linux host and it didn't have Java, so I gave up on X11 forwarding)

我自己(或曾经)在同一个地方,FWIW 我最终签出了 Linux 主机上的 samba 共享,并使用 notepad++ 在 Windows 机器上本地编辑该共享,然后我通过 PuTTY 在 Linux 机器上编译。(我们不允许在 Linux 主机上更新十个 y/o 版本的编辑器,它没有 Java,所以我放弃了 X11 转发)

Now... I run modern Linux in a VM on my Windows host, add all the tools I want (e.g. CDT) to the VM and then I checkout and build in a chroot jail that closely resembles the RTE.

现在...我在我的 Windows 主机上的 VM 中运行现代 Linux,将我想要的所有工具(例如 CDT)添加到 VM,然后我检出并构建一个与 RTE 非常相似的 chroot jail。

It's a clunky solution but I thought I'd throw it in to the mix.

这是一个笨拙的解决方案,但我想我会把它投入到混合中。

回答by Samantha

My solution is similar to the SAMBA one except using sshfs. Mount my remote server with sshfs, open my makefile project on the remote machine. Go from there.

除了使用 sshfs 之外,我的解决方案与 SAMBA 类似。用 sshfs 挂载我的远程服务器,在远程机器上打开我的 makefile 项目。从那里走。

It seems I can run a GUI frontend to mercurial this way as well.

似乎我也可以通过这种方式为 mercurial 运行 GUI 前端。

Building my remote code is as simple as: ssh address remote_make_command

构建我的远程代码非常简单:ssh 地址 remote_make_command

I am looking for a decent way to debug though. Possibly via gdbserver?

不过,我正在寻找一种体面的调试方式。可能通过gdbserver?

回答by 900ss

I had the same problem 2 years ago and I solved it in the following way:

2年前我遇到了同样的问题,我通过以下方式解决了它:

1) I build my projects with makefiles, not managed by eclipse 2) I use a SAMBA connection to edit the files inside Eclipse 3) Building the project: Eclipse calles a "local" make with a makefile which opens a SSH connection to the Linux Host. On the SSH command line you can give parameters which are executed on the Linux host. I use for that parameter a makeit.sh shell script which call the "real" make on the linux host. The different targets for building you can give also by parameters from the local makefile --> makeit.sh --> makefile on linux host.

1) 我使用 makefile 构建我的项目,而不是由 eclipse 管理 2) 我使用 SAMBA 连接来编辑 Eclipse 中的文件 3) 构建项目:Eclipse 使用一个打开与 Linux 的 SSH 连接的 makefile 调用“本地”make主持人。在 SSH 命令行上,您可以给出在 Linux 主机上执行的参数。我为该参数使用了一个 makeit.sh shell 脚本,它在 linux 主机上调用了“真正的”make。您还可以通过本地 makefile --> makeit.sh --> linux 主机上的 makefile 中的参数提供不同的构建目标。

回答by arekolek

I tried ssh -Xbut it was unbearably slow.

我试过了,ssh -X但速度慢得难以忍受。

I also tried RSE, but it didn't even support building the project with a Makefile (I'm being told that this has changed since I posted my answer, but I haven't tried that out)

我也尝试过 RSE,但它甚至不支持使用 Makefile 构建项目(有人告诉我自从我发布答案后这已经改变了,但我还没有尝试过)

I read that NX is faster than X11 forwarding, but I couldn't get it to work.

我读到 NX 比 X11 转发快,但我无法让它工作。

Finally, I found out that my server supports X2Go(the link has install instructions if yours does not). Now I only had to:

最后,我发现我的服务器支持X2Go(如果你的不支持,链接中有安装说明)。现在我只需要:

  • download and unpack Eclipse on the server,
  • install X2Go on my local machine (sudo apt-get install x2goclienton Ubuntu),
  • configure the connection (host, auto-login with ssh key, choose to run Eclipse).
  • 在服务器上下载并解压 Eclipse,
  • 在我的本地机器sudo apt-get install x2goclient上安装 X2Go(在 Ubuntu 上),
  • 配置连接(主机,使用 ssh 密钥自动登录,选择运行 Eclipse)。

Everything is just as if I was working on a local machine, including building, debugging, and code indexing. And there are no noticeable lags.

一切就好像我在本地机器上工作一样,包括构建、调试和代码索引。并且没有明显的滞后。

回答by Gabriel Staples

This answer currently only applies to using two Linux computers [or maybe works on Mac too?--untested on Mac] (syncing from one to the other) because I wrote this synchronization script in bash. It is simply a wrapper around git, however, so feel free to take it and convert it into a cross-platform Python solution or something if you wish

这个答案目前只适用于使用两台 Linux 计算机[或者也可以在 Mac 上运行?--在 Mac 上未经测试](从一台同步到另一台),因为我用 bash 编写了这个同步脚本。git然而,它只是一个包装器,所以如果你愿意,可以随意使用它并将其转换为跨平台的 Python 解决方案或其他东西



This doesn't directly answer the OP's question, but it is so close I guarantee it will answer many other peoples' question who land on this page (mine included, actually, as I came here first beforewriting my own solution), so I'm posting it here anyway.

这并没有直接回答 OP 的问题,但它是如此接近,我保证它会回答许多其他人在此页面上的问题(包括我的问题,实际上,因为我编写自己的解决方案之前首先来到这里),所以我无论如何都要在这里发布。

I want to:

我想要:

  1. develop code using a powerful IDE like Eclipse on a light-weight Linux computer, then
  2. build that code via ssh on a different, more powerful Linux computer (from the command-line, NOT from inside Eclipse)
  1. 在轻量级 Linux 计算机上使用功能强大的 IDE(如 Eclipse)开发代码,然后
  2. 在另一台更强大的 Linux 计算机上通过 ssh 构建该代码(从命令行,而不是从 Eclipse 内部)

Let's call the first computer where I write the code "PC1" (Personal Computer 1), and the 2nd computer where I build the code "PC2". I need a tool to easily synchronize from PC1 to PC2. I tried rsync, but it was insanely slow for large repos and took tons of bandwidth and data.

让我们将我编写代码的第一台计算机称为“PC1”(个人计算机 1),并将我构建代码的第二台计算机称为“PC2”。我需要一个工具来轻松地从 PC1 同步到 PC2。我尝试过rsync,但对于大型存储库来说速度非常慢,并且占用了大量带宽和数据。

So, how do I do it? What workflow should I use? If you have this question too, here's the workflow that I decided upon. I wrote a bash script to automate the process by using gitto automatically push changes from PC1 to PC2 via a remote repository, such as github. So far it works very well and I'm very pleased with it. It is far far far faster than rsync, more trustworthy in my opinion because each PC maintains a functional git repo, and uses far less bandwidth to do the whole sync, so it's easily doable over a cell phone hot spot without using tons of your data.

那么,我该怎么做呢?我应该使用什么工作流程?如果您也有这个问题,这是我决定的工作流程。我编写了一个 bash 脚本,通过使用git远程存储库(例如 github)自动将更改从 PC1 推送到 PC2来自动化该过程。到目前为止,它运行良好,我对它非常满意。它远比 快得多rsync,在我看来更值得信赖,因为每台 PC 都维护着一个功能性的 git 存储库,并且使用更少的带宽来完成整个同步,因此它可以通过手机热点轻松实现,而无需使用大量数据。

Setup:

设置:

  1. Install the script on PC1 (this solution assumes ~/bin is in your $PATH):

    git clone https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
    cd eRCaGuy_dotfiles/useful_scripts
    mkdir -p ~/bin
    ln -s "${PWD}/sync_git_repo_from_pc1_to_pc2.sh" ~/bin/sync_git_repo_from_pc1_to_pc2
    cd ..
    cp -i .sync_git_repo ~/.sync_git_repo
    
  2. Now edit the "~/.sync_git_repo" file you just copied above, and update its parameters to fit your case. Here are the parameters it contains:

    # The git repo root directory on PC2 where you are syncing your files TO; this dir must *already exist* 
    # and you must have *already `git clone`d* a copy of your git repo into it!
    # - Do NOT use variables such as `$HOME`. Be explicit instead. This is because the variable expansion will 
    #   happen on the local machine when what we need is the variable expansion from the remote machine. Being 
    #   explicit instead just avoids this problem.
    PC2_GIT_REPO_TARGET_DIR="/home/gabriel/dev/eRCaGuy_dotfiles" # explicitly type this out; don't use variables
    
    PC2_SSH_USERNAME="my_username" # explicitly type this out; don't use variables
    PC2_SSH_HOST="my_hostname"     # explicitly type this out; don't use variables
    
  3. Git clone your repo you want to sync on both PC1 and PC2.

  4. Ensure your ssh keys are all set up to be able to push and pull to the remote repo from both PC1 and PC2. Here's some helpful links:
    1. https://help.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
    2. https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  5. Ensure your ssh keys are all set up to ssh from PC1 to PC2.
  6. Now cdinto any directory within the git repo on PC1, and run:

    sync_git_repo_from_pc1_to_pc2
    
  7. That's it! About 30 seconds later everything will be magically synced from PC1 to PC2, and it will be printing output the whole time to tell you what it's doing and where it's doing it on your disk and on which computer. It's safe too, because it doesn't overwrite or delete anything that is uncommitted. It backs it up first instead! Read more below for how that works.

  1. 在 PC1 上安装脚本(此解决方案假定 ~/bin 在您的 $PATH 中):

    git clone https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
    cd eRCaGuy_dotfiles/useful_scripts
    mkdir -p ~/bin
    ln -s "${PWD}/sync_git_repo_from_pc1_to_pc2.sh" ~/bin/sync_git_repo_from_pc1_to_pc2
    cd ..
    cp -i .sync_git_repo ~/.sync_git_repo
    
  2. 现在编辑您刚刚在上面复制的“~/.sync_git_repo”文件,并更新其参数以适合您的情况。下面是它包含的参数:

    # The git repo root directory on PC2 where you are syncing your files TO; this dir must *already exist* 
    # and you must have *already `git clone`d* a copy of your git repo into it!
    # - Do NOT use variables such as `$HOME`. Be explicit instead. This is because the variable expansion will 
    #   happen on the local machine when what we need is the variable expansion from the remote machine. Being 
    #   explicit instead just avoids this problem.
    PC2_GIT_REPO_TARGET_DIR="/home/gabriel/dev/eRCaGuy_dotfiles" # explicitly type this out; don't use variables
    
    PC2_SSH_USERNAME="my_username" # explicitly type this out; don't use variables
    PC2_SSH_HOST="my_hostname"     # explicitly type this out; don't use variables
    
  3. Git 克隆要在 PC1 和 PC2 上同步的存储库。

  4. 确保您的 ssh 密钥都设置为能够从 PC1 和 PC2 推送和拉取到远程存储库。以下是一些有用的链接:
    1. https://help.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
    2. https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  5. 确保您的 ssh 密钥都设置为从 PC1 到 PC2 的 ssh。
  6. 现在cd进入 PC1 上 git repo 中的任何目录,然后运行:

    sync_git_repo_from_pc1_to_pc2
    
  7. 就是这样!大约 30 秒后,所有内容都将神奇地从 PC1 同步到 PC2,并且它会一直打印输出,告诉您它在做什么以及它在您的磁盘上的哪个位置以及在哪台计算机上执行。它也很安全,因为它不会覆盖或删除任何未提交的内容。它首先备份它!阅读下面的更多信息,了解它是如何工作的。

Here's the process this script uses (ie: what it's actually doing)

这是该脚本使用的过程(即:它实际在做什么)

  1. From PC1: It checks to see if any uncommitted changes are on PC1. If so, it commits them to a temporary commit on the current branch. It then force pushes them to a remote SYNC branch. Then it uncommits its temporary commit it just did on the local branch, then it puts the local git repo back to exactly how it was by staging any files that were previously staged at the time you called the script. Next, it rsyncs a copy of the script over to PC2, and does an sshcall to tell PC2 to run the script with a special option to just do PC2 stuff.
  2. Here's what PC2 does: it cds into the repo, and checks to see if any localuncommitted changes exist. If so, it creates a new backup branch forked off of the current branch (sample name: my_branch_SYNC_BAK_20200220-0028hrs-15sec<-- notice that's YYYYMMDD-HHMMhrs--SSsec), and commits any uncommitted changes to that branch with a commit message such as DO BACKUP OF ALL UNCOMMITTED CHANGES ON PC2 (TARGET PC/BUILD MACHINE). Now, it checks out the SYNC branch, pulling it from the remote repository if it is not already on the local machine. Then, it fetches the latest changes on the remote repository, and does a hard reset to force the local SYNC repository to match the remote SYNC repository. You might call this a "hard pull". It is safe, however, because we already backed up any uncommitted changes we had locally on PC2, so nothing is lost!
  3. That's it! You now have produced a perfect copy from PC1 to PC2 without even having to ensure clean working directories, as the script handled all of the automatic committing and stuff for you! It is fast and works very well on huge repositories. Now you have an easy mechanism to use anyIDE of your choice on one machine while building or testing on another machine, easily, over a wifi hot spot from your cell phone if needed, even if the repository is dozens of gigabytes and you are time and resource-constrained.
  1. 从 PC1:它检查 PC1 上是否有任何未提交的更改。如果是这样,它会将它们提交到当前分支上的临时提交。然后它会强制将它们推送到远程 SYNC 分支。然后它取消提交它刚刚在本地分支上所做的临时提交,然后通过暂存之前在您调用脚本时暂存的任何文件,将本地 git 存储库恢复到原来的状态。接下来,它将rsync脚本的副本复制到 PC2,并ssh调用告诉 PC2 运行带有特殊选项的脚本,只执行 PC2 的操作。
  2. 这是 PC2 所做的:它cd进入 repo,并检查是否存在任何本地未提交的更改。如果是这样,它会创建一个从当前分支分叉出来的新备份分支(示例名称:my_branch_SYNC_BAK_20200220-0028hrs-15sec<-- 注意这是 YYYYMMDD-HHMMhrs--SSsec),并使用诸如DO BACKUP OF ALL的提交消息向该分支提交任何未提交的更改PC2 上的未提交更改(目标 PC/构建机器). 现在,它检查 SYNC 分支,如果它不在本地机器上,则从远程存储库中提取它。然后,它获取远程存储库上的最新更改,并执行硬重置以强制本地 SYNC 存储库匹配远程 SYNC 存储库。你可以称之为“硬拉”。然而,这是安全的,因为我们已经备份了我们在 PC2 上本地的任何未提交的更改,所以没有任何损失!
  3. 就是这样!您现在已经生成了从 PC1 到 PC2 的完美副本,甚至无需确保干净的工作目录,因为脚本会为您处理所有自动提交和其他内容!它速度很快,并且在巨大的存储库上运行良好。现在,您有一个简单的机制可以在一台机器上使用您选择的任何IDE,同时在另一台机器上构建或测试,如果需要,可以轻松地通过手机的 wifi 热点,即使存储库有几十 GB 并且您是时间和资源受限。

Resources:

资源:

  1. The whole project: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
    1. See tons more links and references in the source code itself within this project.
  2. How to do a "hard pull", as I call it: How do I force "git pull" to overwrite local files?
  1. 整个项目:https: //github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
    1. 在此项目中的源代码本身中查看更多链接和参考。
  2. 如何进行“硬拉”,正如我所说:如何强制“git pull”覆盖本地文件?

Related:

有关的:

  1. git repository sync between computers, when moving around?
  1. 移动时,计算机之间的git存储库同步?

回答by Mohamed Abdelazim

For this case you can use ptp eclipse https://eclipse.org/ptp/for source browsing and building.

对于这种情况,您可以使用 ptp eclipse https://eclipse.org/ptp/进行源代码浏览和构建。

You can use this pluging to debug your application

您可以使用此插件来调试您的应用程序

http://marketplace.eclipse.org/content/direct-remote-c-debugging

http://marketplace.eclipse.org/content/direct-remote-c-debugging