ld cannot find /lib64/libmvec.so.1 with g++ command - c++

I'm getting started learning c++ project building on linux with simple hello world code.
#include <iostream>
int main(){
std::cout<<"hello,world"<<std::endl;
return 0;
}
When I try to run command
g++ hello.cpp
It fails with:
/opt/rh/devtoolset-6/root/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/ld: cannot find /usr/lib64/libmvec_nonshared.a
/opt/rh/devtoolset-6/root/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/ld: cannot find /lib64/libmvec.so.1
But when I try
gcc -lstdc++ hello.cpp
The compilation is successfully finished and generated executable file a.out.
So what is problem here with g++?

Related

Cannot include standard libraries in C++ file

#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.

Getting error clang: error: linker command failed with exit code 1 (use -v to see invocation) while compile C++ file from terminal

I'm getting this error
clang: error: linker command failed with exit code 1 (use -v to see invocation)
while i'm compiling simple cpp file from terminal
gcc hello.cpp
here's the content of hello.cpp file:
#include <iostream>
using namespace std;
// main() is where program execution begins.
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
I think it might conflict with XCode compiler?
gcc hello.cpp should be g++ hello.cpp
gcc is for compiling and linking C code, while g++ is used for C++ code as you have it.
I think it might conflict with XCode compiler?
No. The point is that the gcc command is also able to compile C++ code detected from the .cpp file extension, though the libstdc++.a won't be linked automatically.

Exec Format Error Eclipse CDT

I have a basic C++ program in Eclipse CDT:
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello World!";
}
However, when I try to build it, I get an Exec Format Error. Here is the output produced by the compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o hey.o "..\\hey.cpp"
g++: error: spawn: Exec format error
I am using MinGW Toolchain. I am on 64-bit Windows, and I think that may have something to do with it. Would anyone know how to get this program running?
Edit
Running the exact command in command prompt in the directory where my source file is works just fine, without throwing errors, but it still doesn't work in Eclipse
So, I have fixed this myself by installing the 64-bit version of MinGW (http://sourceforge.net/projects/mingw-w64/). It now compiles and builds noramlly

why the new compiled g++ say "ld: cannot find -lg++"

I wanted a old gcc version 2.7.2.3 with Redhat Linux 9.0 and gcc 2.95.3 and gcc-2.7.2.3.tar.gz, and used the following commands:
./configure --prefix=target-dir
make bootstrap LANGUAGES="c c++" BOOT_CFLAGS="-g -O2"
make install
after that, I can use the gcc-2.7.2.3 to compile c program , but the g++-2.7.2.3 did not work, when I compiled the following simplest c++ program:
//test.cc
int main() {return 0;}
using the command:
g++-2.7.2.3 test.cc
ld: cannot find -lg++
however, I can use the following commands to finish it:
g++-2.7.2.3 -c test.cc
gcc-2.7.2.3 -o test test.o
what should I do for modifying this error? any advice will be appreciated!
Maybe g++ library(lib) directory is missing.

Error: undefined reference to `sqlite3_open'

I'm trying to get started with the C++ API for SQLite.
#include <iostream>
#include <sqlite3.h>
using namespace std;
int main()
{
sqlite3 *db;
if (sqlite3_open("ex1.db", &db) == SQLITE_OK)
cout << "Opened db successfully\n";
else
cout << "Failed to open db\n";
return 0;
}
Compiling this using the command "g++ main.cpp" gives the following error:
/tmp/ccu8sv4b.o: In function `main':
main.cpp:(.text+0x64): undefined reference to `sqlite3_open'
collect2: ld returned 1 exit status
What could have gone wrong? Hasn't sqlite3 properly installed in the server I'm compiling this in?
You need to link the sqlite3 library along with your program:
g++ main.cpp -lsqlite3
You need to adjust your linker flags to link in the sqlite3 library. Libraries are usually installed in /usr/lib or /usr/lib64
Alternatively, you can copy the sqlite3.c file to your project directory and compile it as part of the g++ command:
g++ main.cpp sqlite3.c
as per: http://sqlite.org/cvstrac/wiki?p=HowToCompile
First step: Install all library sqlite3 with the command:
sudo apt-get install libsqlite3-dev
With that you can use #include <sqlite3.h> in a programm of C or C++.
Second step: To compile the program by console:
C++:
g++ program.cpp -o executable -lsqlite3
./executable
C:
gcc program.c -o executable -lsqlite3
./executable
Either link your program to lib g++ yourProgram.c -lsqlite3 in command line or in Open IDE -> project -> properties -> locate lib file for sqlite3 .
Compile using Devcpp
1. add sqlite3.dll file in the project folder.
2. go to Compiler option in Tools >>
3. write sqlite3.dll next to >> Add the following commands when calling compiler
Compile using command line
NOTE : install MinGW (compiler)
g++ file.cpp -o output.exe sqlite3.dll
Compile using VS
define sqlite3.dll in linker in project properties