Compiling SQLite amalgamation file with C++ file using minGW-w64 - c++

I'm just beginning to experiment with C/C++ and SQLite, and installed the minGW-w64 compiler and was able to get the little "Hello, world." code to compile to an .exe file and run it.
Then I added the sqlite3.c amalgamation file to the directory and attempted to compile it with the helloworld.c file, and the first result was many, many rows of errors and warnings, it appears because I used g++ instead of gcc.
I have two very basic questions.
Is it correct to always use the sqlite3.c amalgamation file in this manner, compiling it along with the rest of the code, even though it is a rather large file. The SQLite web site states, "The use of the amalgamation is recommended for all applications" and "makes [them] run faster."
The .exe file was 53kb for hello world compiled alone and 1,015kb for the combination even though no SQLite statements were used.
Why did using g++ generate the large error list and gcc did not generate any? I realize that g++ is for c and c++ and sqlite3 is c. Does this mean that a c++ file and sqlite3.c cannot be compiled together?
Thank you.

Yes, bundling sqlite like that is intended behavior.
You can compile sqlite3.c with a C compiler first and link the result into a binary:
gcc -O2 -c sqlite3.c
g++ -O2 sqlite3.o myapp.cpp -o myapp.exe
I strongly suggest automating this project with a simple makefile or Cmake.

Related

Compiling multiple C++ files in command line?

I'm doing a tutorial right now that doesn't make it to clear on how to do this.
I have two files, int2.cpp which has my main and int1.cpp which is a function (int2 calls on int1) I know they will work but how would one type it into the command line? tutorial says g++ int2.cpp int1.cpp -o int2.cpp, but it says " g++ is an illegal command"
I'm using DOSbox 0.74.
I compile things with tcc sorry but it says -o isn't a command line option
I compile things with tcc sorry but it says -o isn't a command line option
TurboC++ is an obsolete compiler (for an obsolete variant of C++); don't use it.
TinyC (e.g. the tcc command) is a compiler for C, not for C++.
C and C++ are different languages, and you want to learn at least C++11 (since older standards of C++ are obsolete and very different, it is not worth learning them in 2017).
So get and use a free software C++11 compiler like GCC or Clang. BTW both are easily available on most Linux distributions (which I recommend you to use).
Of course you'll want to compile with warnings and debug information, so use
g++ -Wall -Wextra -g with GCC and clang++ -Wall -Wextra -g with Clang.
BTW, you probably want to compile several translation units into a single executable binary. This often involves a linking step (i.e. running the linker on several object files, also using the g++ command). Consider learning to use some build automation tool like GNU make (which has a lot of builtin rules to help in doing that).
g++ is an illegal command means the compiler is not installed on your path. The syntax is (almost) right, you likely just don't have gcc installed.
g++ int2.cpp int1.cpp -o int2.exe

G++ compiler giving "undefined reference" error to functions I have clearly defined

I have referenced several other questions on this site and others addressing this topic and nothing has helped my issue so far.
I have two classes and a main program written in c++. Total 5 files. Everything is written originally in Visual Studio 2013 and compiles and runs there.
All are in a single folder and I use this command to compile them:
g++ myprogram.cpp
I get errors "undefined reference" errors for the contructors and destructors of both classes. Is there something obvious that I am doing wrong? If not I will post my code. Thank you.
When you invoke g++ directly like this, it will do all the steps by default, including linking. Since you haven't listed your other source files, you really just want to do compilation (it would seem) and then link the results of compiling each individual source file. You can use the "-c" option to make it just compile (rather than compile and link). Or, if you do want to build all your source files and link them together, then you should list all of the source files in question. That is:
g++ *.cpp -o yourexecutable # To compile and link all the sources
Or:
g++ -c yourfile.cpp -o yourobjectfile.o # To compile a single source
But really you shouldn't invoke g++ directly at all; there are build systems that provide a layer of abstraction on top of GCC (and other tool chains) that would be a far better, more portable, and simpler approach to building your application. For example, Bazel or Gradle would be better ways to build your program from the commandline. Though not the best or most modern build system, even using Make (and relying on its implicit rules) would be better than direclty invoking the compiler.

Error trying to find exec "cc1d"

I'm trying to compile and run a c++ program from eclipse-c++. Unfortunately, I'm running into quite a few errors. I'm running Linux Mint 17.
My latest error occurs after building, but before the program runs. It seems g++ can't find the executable "cc1d." I've looked on the internet for a solution, but all questions seem to be asking about a missing executable called "cc1." I tried installing gcc-c++ from my package distributor, but it doesn't seem to work. I also tried making a symbolic link pointing to the program "cc." I named the link "cc1d," and it kind worked, but numerous options specified by g++ didn't work when I did that.
Actual error message:
g++: error trying to exec 'cc1d': execvp: No such file or directory
Any thoughts on how to fix this?
This is from the gcc manual:
C++ source files conventionally use one of the suffixes .C, .cc,
.cpp, .CPP, .c++, .cp, or .cxx; C++ header files often use .hh or .H;
and preprocessed C++ files use the suffix .ii. GCC recognizes files
with these names and
compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).
So to identify the code as C++, the source code files must have any of the above mentioned extensions, and not any other extensions such as .D

How do you compile a C++ program with multiple class files from OS X Terminal?

I have tried doing
gcc [folderName] -o [executableName]
but that does not seem to work.
gcc is used for compiling C source files but for C++ you will need to use g++. As for the multiple files, just list them:
g++ [list of all source files] -o [executableName]
Also note that if you have installed gcc via xcode then it will be quite an old version. I would recommend using clang instead as it is the compiler used by xcode and is kept up to date. It works pretty much the same as gcc so for your situation it would be:
clang++ [list of all source files] -o [executableName]
The best way is to use a Makefile as this will only compile files that have changed since the last build.
There is a nice tutorial on using make here

Compile SDL in Cygwin using g++

So for school I made a simple game using the SDL graphics library, and did it in Visual Studios C++. However, I must turn in the source code so that it will compile through Cygwin. I have tried so very hard to figure out the proper way to link everything, but just cannot do it.
Normally I would compile a normal .cpp file through Cygwin something like this:
g++ -W -Wall -pedantic -o test test.cpp
So my questions are:
What is the proper command line command in order to compile (with g++) SDL source code.
Also, how should I folder all the files so that when my instructor gets it, he can run the exact same command as I do?
Should I have the entire SDL library inside with the source code folder? I would like everything grouped so that all he has to do is run the proper command.
The simpler the better :)