Error compiling very simple C program
The line
int main (int argc, char *argv[1]);
should be
int main (int argc, char *argv[])
(without the semi-colon).
Also consider using braces in the if
statement.
Error when compiling some simple c++ code
Normally this sort of failure happens when compiling your C++ code by invoking the C front-end. The gcc
you execute understands and compiles the file as C++, but doesn't link it with the C++ libraries. Example:
$ gcc example.cpp
Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccLTUBHJ.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccLTUBHJ.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in ccLTUBHJ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
_main in ccLTUBHJ.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccLTUBHJ.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccLTUBHJ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
$ g++ example.cpp
$
As you can see, using g++
makes the problems go away. The same behaviour (with slightly different messages) occurs if you use clang
(which I'd recommend):
$ clang example.cpp
Undefined symbols for architecture x86_64:
"std::ios_base::Init::~Init()", referenced from:
___cxx_global_var_init in cc-IeV9O1.o
"std::ios_base::Init::Init()", referenced from:
___cxx_global_var_init in cc-IeV9O1.o
"std::cout", referenced from:
_main in cc-IeV9O1.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in cc-IeV9O1.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in cc-IeV9O1.o
"std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:
_main in cc-IeV9O1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$ clang++ example.cpp
$
As you can see in the clang
error message, you could use -v
to see the linker invocation to see what's going wrong. It would show you this link line:
"/usr/bin/ld" -demangle -dynamic -arch x86_64
-macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o
/var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-hdOL8Z.o
-lSystem /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a
Or something like it - as you can see, it's linking the C runtime, not C++, and also doesn't have the C++ libraries. Using clang++
the link line is:
"/usr/bin/ld" -demangle -dynamic -arch x86_64
-macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o
/var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-wJwxjP.o
/usr/lib/libstdc++.6.dylib -lSystem
/Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a
As you can see, libstdc++
is included, and presto - no link errors.
Error When Compiling a Simple C Program File in Windows 10 CMD Prompt Using MinGW (gcc: error: No such file or directory)
You should not be working under a system folder C:\Windows\System32
. Probably there are access restrictions (e.g. from antivirus) that cause you to run into problems.
I also recommend you don't do it in a path containing spaces or special characters.
Try under a folder C:\Temp
for example.
If you want to try with a more recent MinGW-w64 you can get it from https://winlibs.com/. That site also gives an example on how to use it from the Command Prompt.
If you're new to all this you may also want to consider an IDE like Code::Blocks or Visual Studio Code.
Error while compiling C program with gcc in VSCode
Visual studio code has nothing to do with your issue, you are not compiling with it. Because it is an IDE (or source code editor), not a compiler. I guess you are using it on some Linux or POSIX system. BTW my preferred source code editor is GNU emacs. So your IDE is running some compilation commands (and you need to understand which ones and what these commands are doing). You could run these commands in a terminal (and that actually might be simpler).
As your console logs shows, you are compiling with GCC. Some gcc
command has been started (by Visual studio code probably).
Read carefully about Invoking GCC. Order of arguments matters a lot!
You should compile your code with
gcc -Wall -Wextra -g q2.c -lm -o q2
Let me explain this a bit:
gcc
is your compiler front-end (the actual compiler iscc1
but you never use it directly; you askgcc
to run it)-Wall
asks for almost all warnings-Wextra
asks for extra warnings. You'll be happy to get them-g
asks for debugging information in DWARF. You really want to be able to use thegdb
debugger, andgdb
practically needs debugging information.q2.c
is the source file of your sole translation unit-lm
is for your math library. You are using log(3) and its documentation mention that.-o q2
tellsgcc
to put the executable inq2
(the actual work is done by theld
linker invoked bygcc
)
How to configure visual studio code to use that command is your business. You could otherwise type the above command in a terminal. Then you can run your q2
program by typing ./q2
in a terminal for your shell (and you could use gdb
on it).
Notice that gcc
is starting other programs (like cc1
, as
, ld
). If you want to understand which ones, insert -v
after gcc
in the command above.
Be sure to read the documentation of every function you are using (so read printf(3), scanf(3), log(3) at least...) and of every program you are using (e.g. of gcc
and of Visual studio code).
Once you'll write bigger programs made of several translation units (e.g. foo.c
, bar.c
, gee.c
), you would want to use some build automation tool (because compiling all of them every time with gcc -Wall -Wextra -g foo.c bar.c gee.c -lm -o qqq
is possible, but inconvenient). You could learn to use GNU make (or ninja).
Read How to debug small programs. Don't expect your program to work as you want at first.
BTW, study the source code of some existing free software programs (but start with simple projects, e.g. on github, of less than a hundred thousand lines). This could teach you many useful things.
gcc compile error on simple code
It should be:
struct Image image[10] ;
Or use typedef while defining the struct:
typedef struct {
int w;
int h;
// other code
} Image;
And use the code otherwise same as in your question.
Getting a clang error when compiling a simple C program using GCC
Although you instructed gcc
to put the output of the compilation in ./obj/test_main.o
, you did not tell it to make an object file rather than an executable file. By default, when given a source file, gcc
both compiles and links it. As a result, ./obj/test_main.o
is an executable file, and the second command complains that it was given an executable rather than an object file.
You can fix this by changing the first command to:
gcc ./src/test_main.c -c -o ./obj/test_main.o
The -c
switch instructs gcc
to compile only, not to link.
conflicting types error when compiling c program using gcc
If you don't declare a function and it only appears after being called, it is automatically assumed to be int
, so in your case, you didn't declare
void my_print (char *);
void my_print2 (char *);
before you call it in main, so the compiler assume there are functions which their prototypes are int my_print2 (char *);
and int my_print2 (char *);
and you can't have two functions with the same prototype except of the return type, so you get the error of conflicting types
.
As Brian suggested, declare those two methods before main.
Related Topics
How to Simulate "Press Any Key to Continue"
Difference Between Char* and Char[]
How to Get a "Useful" C++ Binary Search Algorithm
What Is Shared_Ptr's Aliasing Constructor For
Extending the C++ Standard Library by Inheritance
Is the Behaviour of I = I++ Really Undefined
G++ Linking Order Dependency When Linking C Code to C++ Code
Size of Array Passed to C++ Function
How to Include Header Files in Gcc Search Path
Where Is the Lock for a Std::Atomic
Why Does C++ Allow Unnamed Function Parameters
How to Statically Link Standard Library to My C++ Program
Function Pointers Casting in C++
Why Is Including "Using Namespace" into a Header File a Bad Idea in C++