I've written a tiny test file:
#include <stdio.h>
int main() {
printf("hello world\n");
printf("hello world\n");
return 0;
}
And tried to compile the program by
gcc -O0 -g3 -Wall -c -fmessage-length=0
But later gdb produced walls of warning similar like this
bfd_mach_o_scan_read_symtab_symbol: symbol bfd_mach_o_scan_read_symtab_symbol: symbol bfd_mach_o_scan_read_symtab_symbol: symbol bfd_mach_o_scan_read_symtab_symbol: symbol bfd_mach_o_scan_read_symtab_symbol: symbol bfd_mach_o_scan_read_symtab_symbol: symbol bfd_mach_o_scan_read_symtab_symbol: symbol bfd_mach_o_scan_read_symtab_symbol: symbol warning: Could not find object file "/private/var/tmp/Libsyscall/Libsyscall-2422.1.72~6/Libsyscall.build/Libsyscall_dynamic.build/Objects-normal/x86_64/_libc_funcptr.o" - no debug information available for "_libc_funcptr.c".
warning: Could not find object file "/private/var/tmp/Libsyscall/Libsyscall-2422.1.72~6/Libsyscall.build/Libsyscall_dynamic.build/Objects-normal/x86_64/strlen.o" - no debug information available for "strlen.c".
warning: Could not find object file "/private/var/tmp/Libsyscall/Libsyscall-2422.1.72~6/Libsyscall.build/Libsyscall_dynamic.build/Objects-normal/x86_64/strcmp.o" - no debug information available for "strcmp.c".
...
Any idea to solve it?
Related
Compiling a simple hello world program generates warnings when compiling with clang. I understand that using clang-cl will get rid of the warnings.
On Clang's website, it states: "clang-cl is an alternative command-line interface to Clang, designed for compatibility with the Visual C++ compiler, cl.exe."
I do not want to use Microsoft Visual C++'s tool chain. I want to use Clang as the compiler and LLD as the linker.
What is meant by "compatibility with the Visual C++ compiler"?
How do I know which linker is used by default? Clang's documentation says that LLD is used by default, but, if so, then why is there a warning? And why is clang-cl the recommended solution for this warning?
clang
I compiled:
clang main.cpp
and got warnings:
main-a354e7.o : warning LNK4217: locally defined symbol _CxxThrowException imported in function "class std::num_put<char,class std::ostreambuf_iterator<char,struct std::char_traits<char> > > const & __cdecl std::use_facet<class std::num_put<char,class std::ostreambuf_iterator<char,struct std::char_traits<char> > > >(class std::locale const &)" (??$use_facet#V?$num_put#DV?$ostreambuf_iterator#DU?$char_traits#D#std###std###std###std##YAAEBV?$num_put#DV?$ostreambuf_iterator#DU?$char_traits#D#std###std###0#AEBVlocale#0##Z)
main-a354e7.o : warning LNK4217: locally defined symbol __std_terminate imported in function "int `public: __cdecl std::locale::~locale(void)'::`1'::dtor$6" (?dtor$6#?0???1locale#std##QEAA#XZ#4HA)
It still generated an a.exe file. However, this same command generates no file when run in a Debian terminal (WSL Windows Subsystem for Linux) and has errors.
clang && lld [lld-link]
I tried compiling to object code and then passing this to LLD. It resulted in an error:
clang -c main.cpp -o main.o
lld-link main.o
lld-link: error: could not open libcpmt.lib: no such file or directory
What is this library? Where did it come from? Why is it not found?
main.cpp
#include <iostream>
using namespace std;
int add1(int x);
int main()
{
int a;
a = 1;
cout << a << endl; //prints a and goes to new line
a = add1(a);
return 0; //must return 0 to tell the OS the
//program executed successfully
}
int add1( int x )
{
x = x + 1;
return x;
}
clang is the C compiler, clang++ is the c++ one. So to compile as c++, you need clang -c main.cpp -o main.o
clang-cl on the other end is an alternative driver. If you don't want to use the toolchain, don't bother about it. However, if you are like me and try to compile a project that currently compiles with MSVC and want to also compile it with clang, it's really useful.
The main difference, if you don't play with triples is the platform ABI it links to.
Clang++ links against the mingw standard library while clang-cl uses the Microsoft runtime. As a consequence, the name mangling ... is also MSVC compatible. (Of, and it has a permissive mode in which it allows some invalid code for compatibility)
I'm trying to use clang-tidy code analysis so I can check for CppCoreGuidelines. I downloaded LLVM 7.0.0 pre-built binary for Win 7 64 bits. I'm able to successfully compile with clang, I did a basic example compiling this code, I named the source test.cpp:
// test.cpp
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout << "Hello World!" << std::endl;
return 0;
}
Then I ran this in the terminal:
clang test.cpp
I got this output when compiling:
test-c4b051.o : warning LNK4217: locally defined symbol __std_terminate imported in function "int `public: static unsigned __int64 __cdecl std::char_traits<char>::length(char const * const)'::`1'::dtor$2" (?dtor$2#?0??length#?$char_traits#D#std##SA_KQEBD#Z#4HA)
test-c4b051.o : warning LNK4217: locally defined symbol _CxxThrowException imported in function "public: void __cdecl std::ios_base::clear(int,bool)" (?clear#ios_base#std##QEAAXH_N#Z)
But it worked fine printing "Hello World" and everything goes fine until here, but when I want to run clang-tidy I get the following output when I run this, I took the reference from here Extra Clang Tools 8 documentation:
clang-tidy test.cpp -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus*
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "test.cpp"
No compilation database found in C:\Users\uidr8361\Desktop\C++ or any parent directory
fixed-compilation-database: Error while opening fixed database: no such file or directory
json-compilation-database: Error while opening JSON database: no such file or directory
Running without flags.
I read this thread but this seems to apply for clang compilation and I don't know if this also applies for clang extra tools, clang-tidy in particular:
How to compile Clang on Windows
Just put -- (minus minus) on the command line at the end
clang-tidy -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus* test.cpp --
You would normally put your cl,gcc,clang arguments afterwards
clang-tidy -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus* test.cpp -- -DDEBUG -I./include
I am 100% new at c++ so bear with me :)
I am getting an error with this file and not sure why. any help is appreciated.
#include <iostream>
using namespace std;
int main()
{
cout << "hi" << endl;
return 0;
}
------------ Build: Debug in 1600 (compiler: GNU GCC Compiler)-------------
g++ -o bin/Debug/1600 obj/Debug/main.o obj/Debug/src/test.o obj/Debug/test03.o
duplicate symbol _main in:
obj/Debug/main.o
obj/Debug/test03.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
From the source files symbols are created. main in the .cpp file becomes _main as a symbol. During linking there can be only one main function, hence only one _main symbol is expected and allowed.
You have three object files that were created:
obj/Debug/main.o which contains main
obj/Debug/src/test.o
obj/Debug/test03.o which also contains main
Probably because you have a .cpp file for each of them and the command line or IDE you are using asked for them all to be compiled.
duplicate symbol _main
The text above is telling you that the linker (trying to make sense of all the compiled object (.o) files) found more than one main.
So the solution is to look at your IDE settings and remove the other files (or at least remove main from the other files) because you are only interested in compiling the one source file.
Its hard to tell what you're running from the question.
Here is how to build a simple C++ program using gcc
In
my_program.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "hi" << endl;
return 0;
}
To compile to object files type
g++ -c my_program.cpp
To link (you'd normally have more files here)
g++ -o my_program my_program.o
So, this isn't very fun so most people use a build system like make, cmake, msbuild or whatever the CLion IDE uses.
I am trying to run a program preloading my library using LD_PRELOAD. At runtime the program is throwing following error.
ld.so.1: gdbser64: fatal: relocation error: file libmy.so: symbol
_ZN10__cxxabiv118register_exit_codeEPFYvvE: referenced symbol not found
libmy.so is not using register_exit_code symbol anywhere. It might be case that a standard library linked by libmy.so is using the function. But I am not able to find who is using this symbol or who has defined it.
It is on Solaris, compiled using CC(solaris cpp compiler).
Are all your source files/libs built with -std=c++0x?
Otherwise, the Oracle docs suggest that if you are linking with -lstdc++ then you should use -lstdc++ -lgcc_s -lCrunG3
my code
inline int DOFILE(string& filename) {
printf("lua_open\n");
/* initialize Lua */
lua_State* L = lua_open();
printf("lua_openlibs\n");
/* load Lua base libraries */
luaL_openlibs(L);
printf("lua_dofile\n");
/* run the script */
int ret = luaL_dofile(L, filename.c_str());
printf("lua_close\n");
/* cleanup Lua */
lua_close(L);
return ret;
}
compile options:
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall", "-llua-5.1"]
also tried '-llua', '-llualib', all of them report warning
i686-apple-darwin11-llvm-g++-4.2: -llua-5.1: linker input file unused because linking not done
When I run, it report:
lua_open
dyld: lazy symbol binding failed: Symbol not found: _luaL_newstate
Referenced from: /Users/gl/workspace/node-lua/build/Release/node_lua.node
Expected in: flat namespace
dyld: Symbol not found: _luaL_newstate
Referenced from: /Users/gl/workspace/node-lua/build/Release/node_lua.node
Expected in: flat namespace
You should be using the obj.ldflags parameter for libraries.
The build tool you are using produces its binaries in two steps:
compile
link
The compile step uses the obj.cxxflags compiler flags. Libraries are not needed to compile, so passing linker flags (-lfoo) in there is no useful - the compiler doesn't use them at all (hence the warnings).
The link step should use both obj.cxxflags and obj.ldflags. (ld is the name of the linker.)
(It is not uncommon for very simple code to do both compiling and linking at the same time, e.g. with g++ -o thing thing.cpp -lpthread. But for larger builds, separating compiling and linking is usual.)