I'm using clang 3.7 with MinGW-w64 version 4.0.
Attempting to build this:
#include <thread>
void f(){}
int main()
{
std::thread t(f);
return 0;
}
with these compiler flags clang++ -std=c++11 -pthread C:\Users\Илья\Desktop\main.cpp -o C:\Users\Илья\Desktop\main.exe -static
produces this linker output:
C:\mingw64\lib\gcc\x86_64-w64-mingw32\5.1.0\libstdc++.a(tinfo.o):(.text$_ZNKSt9type_infoeqERKS_+0x0):
multiple definition of std::type_info::operato r==(std::type_info const&) const'
C:\Users\75BD~1\AppData\Local\Temp\main-005548.o:(.text[_ZNKSt9type_infoeqERKS_]+0x0):
first defined here
C:\mingw64\lib\gcc\x86_64-w64-mingw32\5.1.0\libstdc++.a(future.o):(.text$_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE10_M_destroyEv[_ZNSt16
_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE10_M_destroyEv]+0x0): multiple definition of
std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_de stroy()'
C:\Users\75BD~1\AppData\Local\Temp\main-005548.o:(.text[_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE10_M_destroyEv]+0x0):
first defined her e
C:\mingw64\lib\gcc\x86_64-w64-mingw32\5.1.0\libstdc++.a(future.o):(.text$_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE10_M_releaseEv[_ZNSt16
_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE10_M_releaseEv]+0x0): multiple definition of
`std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_re lease()'
C:\Users\75BD~1\AppData\Local\Temp\main-005548.o:(.text[_ZNSt16_Sp_counted_baseILN9__gnu_cxx12_Lock_policyE2EE10_M_releaseEv]+0x0):
first defined her e clang++.exe: error: linker command failed with
exit code 1 (use -v to see invocation)
How can I fix this and build this app with -static and -pthread?
Related
I have these three files:
test.h
inline int foo(int i) {
return i;
}
asdf1.cpp
#include "test.h"
int main () {
}
asdf2.cpp
#include "test.h"
int bar () {
return 42;
}
When compiling with g++ asdf1.cpp asdf2.cpp, this happens:
$ g++ asdf1.cpp asdf2.cpp
/usr/bin/ld: /tmp/ccTPkxPK.o: in function `foo(int)':
asdf2.cpp:(.text+0x0): multiple definition of `foo(int)'; /tmp/ccJLbLeL.o:asdf1.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
$ g++ asdf1.cpp asdf2.cpp -O1
$ g++ asdf1.cpp asdf2.cpp -Wpedantic
$ g++ asdf1.cpp asdf2.cpp -std=c++14
$ # all of the above work
Why does g++ ignore the inline keyword unless some unrelated flag is given?
Especially weird since, with the -v flag, g++ tells me that it's using c++14 even when nothing is specified. Also, as far as I'm aware, the inline keyword (in this usage) exists in every C++ standard.
File first_module.cppm
export module first_module;
int foo(int x) {
return x;
}
export int e = 42;
export int bar() {
return foo(e);
}
Pre-compiling (no problems):
$ clang++ --std=c++20 -fmodules --precompile first_module.cppm -o first_module.pcm
Compiler information:
$ clang++ -v
clang version 10.0.0
Target: x86_64-pc-windows-msvc
File first-main.cc
import first_module;
int main() {
return bar();
}
Compiling (no problems):
$ clang++ --std=c++20 -fmodules first-main.cc -fmodule-file=first_module.pcm first_module.pcm
Everything is ok.
File second-main.cc
import first_module;
#include <iostream>
int main() {
std::cout << bar() << std::endl;
}
Compiling same way:
$ clang++ --std=c++20 -fmodules second-main.cc -fmodule-file=first_module.pcm first_module.pcm
Result: ton of errors like:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\eh.h:56:14: error: reference to 'type_info' is ambiguous
_In_ type_info const& _Type,
^
note: candidate found by name lookup is 'type_info'
note: candidate found by name lookup is 'type_info'
I have feeling that I am doing something wrong, because I have newest MSVS (updated recently), newest clang, but something still not working on Windows on trivial examples.
Or may be this is known bug? Tried to google it, no results.
The core problem resides in that your current Clang installation it's working with the MSVC toolchain and there's a known issue when you compile using #include directives with the Microsoft toolchain with Clang using the modules feature.
You can find it here, opened since 2018
https://github.com/llvm/llvm-project/issues/38400
I am using pre-built LLVM/Clang 3.8.0 binaries on Ubuntu 16.04.2, 64 bit. I tried to lift a minimal program to LLVM IR, then link the IR to DataFlowSanitizer libraries to produce executable code. In the second step, the process throws a bunch of linker errors.
#include <sanitizer/dfsan_interface.h>
#include <assert.h>
int main(void) {
int i = 1;
dfsan_label i_label = dfsan_create_label("i", 0);
dfsan_set_label(i_label, &i, sizeof(i));
return 0;
}
clang -c -emit-llvm -fsanitize=dataflow test2.c -o test2.bc
clang -fsanitize=dataflow test2.bc -o test2
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/Scrt1.o:
In function _start':(.text+0x20): undefined reference tomain'
/tmp/test2-c642ef.o: In function dfs$main':
test2.bc:(.text+0x96): undefined reference todfs$dfsan_create_label'
test2.bc:(.text+0xeb): undefined reference to dfs$dfsan_set_label'
/tmp/test2-c642ef.o: In functiondfs$dfsw$dfsan_create_label':
test2.bc:(.text+0x16e): undefined reference to
dfs$dfsan_create_label'
/tmp/test2-c642ef.o: In functiondfs$dfsw$dfsan_set_label':
test2.bc:(.text+0x1e4): undefined reference to `dfs$dfsan_set_label'
clang-3.8: error: linker command failed with exit code 1 (use -v to
see invocation)
Any idea what might I be doing wrong?
I've been trying to compile with std::atomic, and I'm getting unresolved references to __atomic_load, __atomic_store, and __atomic_store_16.
I know in a later version of gcc (4.8+?) you include -latomic, but I'm compiling with gcc 4.7.3; I've tried adding -latomic_ops and -latomic_ops_gpl, but neither seem to do much.
I am installing gcc 4.8.1 now, but I do have a release platform that'll really need to be compiled for 4.7.3.
Many thanks.
Edit:
Ok, here's some code that results in the problem I have:
atomics.cpp
#include <atomic>
#include <stdint.h>
struct dataStruct {
int a;
uint16_t b;
float c;
dataStruct(int ai, uint16_t bi, float ci) noexcept : a(ai), b(bi), c(ci) {
}
dataStruct() noexcept : dataStruct(0,0,0) {
}
};
int main() {
std::atomic<dataStruct> atomicValue;
atomicValue = dataStruct(10, 0, 0);
return atomicValue.load().b;
}
With "g++-4.8.1 *.cpp -std=c++0x -latomic", this compiles fine.
With "g++-4.7.3 *.cpp -std=c++0x -pthread -lpthread -latomic_ops", it fails with the following:
/tmp/ccQp8MJ2.o: In function `std::atomic<dataStruct>::load(std::memory_order) const':
atomics.cpp:(.text._ZNKSt6atomicI10dataStructE4loadESt12memory_order[_ZNKSt6atomicI10dataStructE4loadESt12memory_order]+0x2f): undefined reference to `__atomic_load'
/tmp/ccQp8MJ2.o: In function `std::atomic<dataStruct>::store(dataStruct, std::memory_order)':
atomics.cpp:(.text._ZNSt6atomicI10dataStructE5storeES0_St12memory_order[_ZNSt6atomicI10dataStructE5storeES0_St12memory_order]+0x35): undefined reference to `__atomic_store'
collect2: error: ld returned 1 exit status
Ok, finally found the answer at: https://gcc.gnu.org/wiki/Atomic/GCCMM
Turns out, 4.7 did not in fact have 'official' atomics support (just the header files). If you want to use atomics in 4.7 compilers, you must download the source code linked on that page and build it yourself
gcc -c -o libatomic.o libatomic.c
ar rcs libatomic.a libatomic.o
Then, you can build it using
g++-4.7.3 -std=c++0x atomics.cpp -latomic -L./
I'm on Ubuntu Trusty and I'm building the following code with this command. I'm new to clang and I could use help debugging these errors. The cpp and hpp files these C bindings are from compile fine. I copied the headers from those files into the below file I have listed here, w.cpp
clang -std=c++11 w.cpp -o w `pkg-config --cflags --libs opencv`
Here are the errors:
/usr/bin/ld: /tmp/w-2a90f4.o: undefined reference to
symbol '_ZNSt8ios_base4InitD1Ev##GLIBCXX_3.4'
//usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding
symbols: DSO missing from command line
clang: error: linker command failed with exit code 1
(use -v to see invocation)
Here is the contents of w.cpp. I'm trying to benchmark code using
clang.
#include <opencv2/c/mat.hpp>
#include <opencv2/c/opencv_generated.hpp>
using namespace cv;
using namespace std;
using namespace flann;
using namespace cvflann;
void cv_delete(char* self) {
delete self;
}
Mat* cv_create_Mat() {
return new Mat();
}
BFMatcher* cv_create_BFMatcher(int normType, bool crossCheck) {
return new BFMatcher(normType, crossCheck);
}
int main () {
for( int a = 1; a < 20; a++)
{
Mat a = &cv_create_Mat();
cv_delete(a);
}
}
I had the same error using gcc instead of g++, I believe you must use clang++
clang++ -std=c++11 w.cpp -o w `pkg-config --cflags --libs opencv`
While compiling C++ files, its better to use
g++ client.cpp -Wall -lstdc++ -o client