问题
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:
Under the
Projects
tab on the left, right- click on your project and clickClose project
. Do this for all projects that are open.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:
- To create the file, click on
File
->New
->Empty file
- After typing the code, save it with
.c
extension. - 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:
- create a new project
- open that project and then run it. It works of course
- 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 - close all projects in menu:
file
- close all files in menu
file
- 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 (= orF9
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