How to compile and run a C file in CodeBlocks which is outside a project?

跟風遠走 提交于 2019-12-07 23:10:21

问题


I have opened a project in CodeBlocks, which builds and runs successfully. Now if I open another C file in the IDE, the build and run command will act on the existing open project and not on the new C file. If there were multiple projects, we can switch between them using " activate project" . But what if there is one active project and another C file outside the project. How to complile and run the C file then ?


回答1:


  1. Under the Projects tab on the left, right- click on your project and click Close project. Do this for all projects that are open.

  2. Now, if your C file is open, just press F9 to compile and run your program.

It is easy to compile and run a single C file in Code::Blocks:

  1. To create the file, click on File -> New -> Empty file
  2. After typing the code, save it with .c extension.
  3. Press F9 to compile and run the program.



回答2:


Code::Blocks is not a compiler, but an IDE.

How to compile and run a C file in CodeBlocks which is outside a project?

It runs compilation commands, probably using GCC (but consider also Clang). It is the compiler which compiles your code (not CodeBlock).

So you don't compile with CodeBlocks.

(BTW, remove gcc on your system, and CodeBlocks become useless to build any program -both inside a project or single file- from C code)

To compile foo.c into some program fooprog you want to run something like

gcc -Wall -Wextra -g foo.c -o fooprog

and you may want other arguments to gcc (e.g. some -I or -D for preprocessing, some -L or -l for linking libraries). Their order is important. Read chapter on invoking GCC. The -Wall -Wextra asks for all warnings and more of them. The -g asks for debug information. The -o fooprog requires to output the executable fooprog... Details could be operating system specific.

You can run that gcc command in a terminal. Perhaps CodeBlock could be configured to run it somehow (that is a very different question).

You could also learn more about build automation. Consider GNU make or ninja, etc... Your Makefile might build several executables (with a plain make command, and you could configure your IDE or editor to run it).

Notice that many free software projects (on github, sourceforge, etc etc...) are not requiring any particular IDE (but are built using some build automation software). That could ring bells in your head. Good source code editors (such as emacs or vim) are able to run build commands (as nicely, and more generally, than IDEs). Perhaps you should consider using them.




回答3:


If the file is not part of a project, I doubt you could compile it with any IDE. You probably need to either compile it manually or create a project for it.

IDEs are created to manage entire projects, not a single file.




回答4:


With a little trick you can.

Follow me for a test version:

  1. create a new project
  2. open that project and then run it. It works of course
  3. open your single file, at this point, codeblock is involved the active project, and if you switch to your single file and press: Ctrl + F9 it still builds your project and NOT your single file
  4. close all projects in menu: file
  5. close all files in menu file
  6. reopen your project and your single file

Now on your project hit:

  • Ctrl + F9 for build
  • and then Ctrl + F10 for run it will run (= or F9 for both)

Also try this on your single file, then your single file will run as well.


have been tested on Code::Blocks 16.04; 32-bit; Ubuntu



来源:https://stackoverflow.com/questions/46386997/how-to-compile-and-run-a-c-file-in-codeblocks-which-is-outside-a-project

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