coddding
coddding

Reputation: 343

C++ GCC - Unable to include ctime, cstring or cstdlib

So I have created a simple function to get the current date and time. It works fine when I am testing it in Visual Studio on my Windows machine.

These are the directories included in order for my date time function to work:

#include <ctime>
#include <cstring>
#include <cstdlib>

Adding these to the top of my c++ program in Visual Studio will work fine, but when I try to compile it using GCC on my VPS server, I get this error

main.c:12:17: error: ctime: No such file or directory
main.c:13:19: error: cstring: No such file or directory
main.c:14:19: error: cstdlib: No such file or directory

Here is how I compile my C++ program on my VPS server using GCC:

gcc main.c -o bin/main `mysql_config --cflags --libs` -std=c99 -lpthread;

Upvotes: 5

Views: 14839

Answers (3)

BusyProgrammer
BusyProgrammer

Reputation: 2781

Your code is of C++; however, there are 2 mistakes out of your code that are leading to these errrors:

Firstly, your file with the code should be named with a .cpp or .cxx extension at the end of it. Right now, it is main.c, and .c is an extension for a file of C code, not C++. So, you should change your file name to main.cpp.

Secondly, put simply, gcc is a compiler meant for C code. Your code, once again, is C++. Therefore, use g++ in your command, not gcc.

Upvotes: 2

gcc is a C compiler. To compile C++ code you need to use g++.

Your source file should also have a .cpp or .cxx extension.

Upvotes: 3

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33904

gcc is not the compiler for c++. you need to use g++.

See a live example here.

You will also need to review your flags and -std type. You are trying to compile a c++ file as c.

Upvotes: 6

Related Questions