Eclipse C/C++ project on Bash on Ubuntu on Windows

↘锁芯ラ 提交于 2019-12-06 23:47:23

问题


I've installed Eclipse Neon (v4.6). Also, I have "Bash on Ubuntu on Windows" (Sept '16). I installed gcc and gdb for compiling and debugging in C. I tested them on terminal and they're running.

I'm trying to use Eclipse to create a new C project but in the compiler selection stage it doesnt give me the option to use the "Bash on Ubuntu on Windows" compiler.

Is there any way to solve my problem?


回答1:


We use Eclipse and a makefile project. I configured Eclipse by adding an "external builder" - and use the following script:

-c "cd `echo '${project_loc}' | sed  -e 's/C:/\/mnt\/c/g' -e 's/\\\\/\//g'` && make -f path_to_makefile/makefile

The "cd" and sed trick is to convert from Eclipse "C:\" to Windows Subsystem for Linux "/mnt/c/" and replace "\" with "/"

Make now executes in the project location as defined by ${project_loc}

I still have not figured out how to execute gdb on Linux. But at least compile+link (and execute unit tests as defined in my makefile) is now leveraging the full speed of the native Ubuntu toolchain...




回答2:


Because Bash/WSL is pretty new and the ability to correctly invoke Linux commands from outside Bash (using bash.exe -c ...) is not well understood by many dev tools (yet), you may have to do some work to define custom tasks/actions to call the necessary bash.exe -c "gcc ..." commands that you want to invoke.

However, you should absolutely be able to write/configure a makefile (or, perhaps even better, a CMake file) which can switch paths to lib/include/tools/etc. based on environment and then build smoothly on Windows or in Linux.

This very scenario has sparked an idea for a cool blog post that I'll work on and publish to our blog https://blogs.msdn.microsoft.com/commandline in the next week or two :)

HTH.




回答3:


I think the best way to use the wsl for programming in C/C++ is like Microsoft does it. Visual Studio use ssh to connect to linux environment for "C++ for Linux Development". So, in our case we can set up openssh server on wsl, connect to localhost from your IDE and start remote project.

  • Eclipse and NetBeans supports remote projects.
  • Clions I think not.
  • Here are Microsoft's instruction on how to configure Visual Studio and wsl: https://blogs.msdn.microsoft.com/vcblog/2017/02/08/targeting-windows-subsystem-for-linux-from-visual-studio/

May 2017




回答4:


I have some better way... You can add new user terminal and give "Ubuntu.exe" location in eclipse under Local terminal setup as shown in screen shot.

in my case directory is ...

  • C:\Users\MyuserName\AppData\Local\Microsoft\WindowsApps\CanonicalGroupLimited.Ubuntu16.04onWindows_79rhkp1fndgsc

if eclipse not able to select ubuntu16.4.exe, then write manually in path. as shown in image.

Now just click on Apply and close. Open terminal (Ctrl+Alt+Shift+T) and select Ubuntu terminal.

Now, Navigate to source directory through terminal and compile you project. Enjoy...




回答5:


Initially I followed espenalb's advice, but it didn't parse my build output correctly: double-clicking on lines didn't open the file, nor go to the line with the error.

I ended up changing the built-in CDT builder (instead of adding an external builder). I changed the build command to be the following (alex is my username):

bash.exe -c make 2>&1 | sed -e 's/\\/home\\/alex/C:\\\\Users\\\\alex/g' -e 's/\\//\\\\/g'

This way, the make output is converted from paths with "/home/alex" to the same path under "c:\users\alex". This fixes the parsing issue.

I also had an issue with eclipse replacing the build output with "Build not configured correctly" - I fixed it by disabling the "Scanner Configuration Builder" in the "Builders" submenu of the project properties (it's a C Makefile project). Also, need to remove targets "all" and "clean" from default build command.



来源:https://stackoverflow.com/questions/39670891/eclipse-c-c-project-on-bash-on-ubuntu-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!