c++ program compile no reference to main - c++

i am trying to compile my c++ program and I am receiving this error.
g++ -c main.cpp
g++ -o main.o account.o checkingaccount.o savingsaccount.o -o main
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
This is the makefile I am using:
all: main
main: main.o account.o checkingaccount.o savingsaccount.o
g++ -o main.o account.o checkingaccount.o savingsaccount.o -o main
main.o: main.cpp checkingaccount.h savingsaccount.h account.h
g++ -c main.cpp
account.o: account.cpp account.h
g++ -c account.cpp
checkingaccount.o: checkingaccount.cpp checkingaccount.h account.h
g++ -c checkingaccount.cpp
savingsaccount.o: savingsaccount.cpp savingsaccount.h account.h
g++ -c savingsaccount.cpp
~

This line:
g++ -o main.o account.o checkingaccount.o savingsaccount.o -o main
... shouldn't have -o at the beginning. This specifies main.o as the name of the output file and does not link it. The second -o overrides this, but the file still isn't linked. Assuming main.cpp has your int main() function, you're not linking it in.

Related

Why gcc cant complle this file containing virtual class ,but clion IDE can do that? [duplicate]

Shall this be the example:
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
It throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Also, this example:
#include <iostream>
int main()
{
std::cout << "Hola, moondo.\n";
}
throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Note: I am using Debian 7 (Wheezy).
Compile the program with:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
as cout is present in the C++ standard library, which would need explicit linking with -lstdc++ when using gcc; g++ links the standard library by default.
With gcc, (g++ should be preferred over gcc)
gcc main.cpp -lstdc++ -o main.o
Yes, using g++ command worked for me:
g++ my_source_code.cpp
Assuming code.cpp is the source code, the following will not throw errors:
make code
./code
Here the first command compiles the code and creates an executable with the same name, and the second command runs it. There is no need to specify g++ keyword in this case.
Makefiles
If you're working with a makefile and you ended up here like me, then this is probably what you're looking or:
If you're using a makefile, then you need to change cc as shown below
my_executable : main.o
cc -o my_executable main.o
to
CC = g++
my_executable : main.o
$(CC) -o my_executable main.o
Adding the following line in your CMake makes gcc link with std and therefore recognize std::cout
target_link_libraries(your_project
PRIVATE
-lstdc++
)
FWIW, if you want a makefile, here is how you can do either answer by switching the compiler at the top.
# links stdc++ library by default
# CC := g++
# or
CC := cc
all: hello
util.o: util.cc
$(CC) -c -o util.o util.cc
main.o: main.cc
$(CC) -c -o main.o main.cc
# notice -lstd++ is after the .o files
hello: main.o util.o
$(CC) -o hello main.o util.o -lstdc++
clean:
-rm util.o main.o hello

Generating .so file

I have file main.c, I want to create main.so.
main.c depends on libCmodel.so file.
main.c looks like
#include <stdio.h>
#include "Cmodel.h" // Must include before Amodel and Bmodel
#include "Amodel.h"
#include "Bmodel.h"
int main(){
..
return 0;
}
It can be observed that, libCmodel.so file depends on libAmodel.so.4 and libBmodelso.11.
All the "*.so" files are present in the same directory as main.c
I used the following command to generate object file
gcc -x c++ -fPIC main.c -o main.o -c -I.
I see that main.o is generated.
But, I am not sure if I have used the correct command. Is the above command correct?
Then, I tried the following commads to generate main.so ,
g++ -shared -o main.so main.o -L. -lCmodel
g++ -shared -o main.so main.o -L. -lCmodel -lAmodel -lBmodel
g++ -shared -o main.so main.o -L. -lCmodel -lAmodel.4 -lBmodel.11
g++ -shared -o main.so main.o -L. -lCmodel -lAmodel.so.4 -lBmodel.so.11
Which one of the above commands is correct?
Please help
The file containing your main function should not be a shared library. It is the entry point for an application, so it should be linked as an executable.
gcc -x c++ -I. -L. -o main main.c -lCmodel

makefile error no such file or directory

So this is my first time making a make file so its really basic. I have 2 cpp files (functions.cpp and main.cpp) and 2 header files (structDeclaration.h and Prototypes.h). It needs to be able to compile my program but if only one file changes then it shouldn't recompile the entire thing.
heres my error:
g++ -c gradebook main.o Functions.o -I.
g++: error: gradebook: No such file or directory
make: *** [gradebook] Error 1
and heres my makefile:
CC = g++
gradebook: main.o Functions.o
g++ -c gradebook main.o Functions.o -I.
main.o: main.cpp Prototypes.h structDeclaration.h Prototypes.h
g++ -c main.cpp
Functions.o: Functions.cpp structDeclaration.h
g++ -c Functions.cpp
the commands have to be valid commands. I think you mean
g++ -o gradebook main.o Functions.o
If in doubt just try typing the command you are asking make to run for you, there is no magic involved here

/src/DB/sqlite3.dll: file not recognized: File format not recognized

I am trying to include a sqlite3 in my cpp project but while compilation it gives below error:
g++ -c -std=c++11 -g src/Main.cpp -I"C:/Mycode/src/DB"
-L"C:/Mycode/src/DB" -lsqlite3
g++ -g -o Main.exe Main.o Data.o SqliteApi.o -lws2_32
-L"C:/Mycode/src/DB" -lsqlite3
C:/Mycode/src/DB/sqlite3.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
make: ***[Mycode.exe] Error 1
I feel during final linking time it gives error.
I am using my own make file for compilation, below is the make command I am using:
DB_DIR="C:/Mycode/src/DB"
clean:
rm Main.o Main.exe
Main.exe: Main.o Data.o SqliteApi.o
g++ -g -o Main.exe Main.o Data.o SqliteApi.o -lws2_32 -L${DB_DIR} -lsqlite3
Main.o: src/Main.cpp
g++ -c -std=c++11 -g src/Main.cpp -I${DB_DIR} -lsqlite3
Data.o: src/Data.cpp
g++ -c -std=c++11 -g src/Data.cpp
SqliteApi.o: src/SqliteApi.cpp
g++ -c -std=c++11 -g src/SqliteApi.cpp
I have googled but I couldn't find any solution or suggestion for this error.
Any help will be much appreciated.
The recommended way of using the SQLite library is to compile the amalgamation source file directly into your program:
Main.exe: Main.o Data.o SqliteApi.o
g++ -g -o Main.exe Main.o Data.o SqliteApi.o sqlite3.o -lws2_32
sqlite3.o: src/sqlite3.c
gcc -c ... src/sqlite3.c

undefined reference to `EVP_CIPHER_CTX_init'

HI,
I have an error: undefined reference to EVP_CIPHER_CTX_init'. I have the encryption code in the header.cpp. In header.h i've initialized the method that i use for encryption. i've created an ar lib.a from header.cpp and header.h. i've added the -lcrypto but still when i run the test.cpp code where i've added in the header the header.h and i compile g++ test.cpp -o test -lib.a i have this error. where am i wrong?
the compilation code:
g++ -c -static -L -lb header.cpp -lcrypto -o lib.o
ar rcs lib.a lib.o
g++ test.cpp -o tst libr.a
in the header.h i did include all the libraries necesarry for the openssl/evp.h and so on. In test.cpp i've included header.h file.