g++ undefined reference to `main' - gcc

I have a gcc 5.2.0 configured as follows :
Using built-in specs.
COLLECT_GCC=gcc-5.2.0
COLLECT_LTO_WRAPPER=/usr/local/lvm/gcc-5.2.0/libexec/gcc/x86_64-unknown-linux-gnu/5.2.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr/local/lvm/gcc-5.2.0 --enable-checking=release --with-gmp=/usr/local/lvm/gmp-6.0.0 --with-mpfr=/usr/local/lvm/mpfr-3.1.2 --with-mpc=/usr/local/lvm/mpc-1.0.3 --enable-languages=c,c++,fortran,objc,obj-c++ --with-isl=/usr/local/lvm/isl-0.14 --with-cloog=/usr/local/lvm/cloog-0.18.4 --program-suffix=-5.2.0
Thread model: posix
gcc version 5.2.0 (GCC)
and g++ :
Using built-in specs.
COLLECT_GCC=g++-5.2.0
COLLECT_LTO_WRAPPER=/usr/local/lvm/gcc-5.2.0/libexec/gcc/x86_64-unknown-linux-gnu/5.2.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr/local/lvm/gcc-5.2.0 --enable-checking=release --with-gmp=/usr/local/lvm/gmp-6.0.0 --with-mpfr=/usr/local/lvm/mpfr-3.1.2 --with-mpc=/usr/local/lvm/mpc-1.0.3 --enable-languages=c,c++,fortran,objc,obj-c++ --with-isl=/usr/local/lvm/isl-0.14 --with-cloog=/usr/local/lvm/cloog-0.18.4 --program-suffix=-5.2.0
Thread model: posix
gcc version 5.2.0 (GCC)
I have the following simple c++ code in tmp2.cpp file :
extern "C" {
double mysum(double x, double y)
{
return x+y;
}
}
that I am trying to compile into a dynamic library (.so) as follows :
export LD_LIBRARY_PATH=/usr/local/lvm/gmp-6.0.0:/usr/local/lvm/mpfr-3.1.2:/usr/local/lvm/mpc-1.0.3:/usr/local/lvm/cloog-0.18.4:/usr/local/lvm/isl-0.14/lib:/usr/local/lvm/gcc-5.2.0/lib64
export PATH=/usr/local/lvm/gcc-5.2.0/bin/:$PATH
g++-5.2.0 -m32 -Wall -g -c ./tmp2.cpp
g++-5.2.0 -m32 -dynamiclib ./tmp2.o -o ./tmp2.so
and the last command gives me the following error :
/usr/lib/../lib32/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
The detail output thx to -v can be found in a gist here.
I am quite new to gcc/g++ and don't really get what is going on. What's happened ?

You should use -shared option of g++ for creating a shared object.
g++-5.2.0 -m32 -shared -dynamiclib ./tmp2.o -o ./tmp2.so

Related

libgomp and MacOsx

I am using MacOS Mojave version 10.14.16 and I am trying to install http://www.rpl2.net/index.php (a programming language using Reverse Polish Notation as found on some HP calculators like the 48GX), supposed to work on MacOSX (they say "MacOS X (Xcode 3.1.4 + gfortran 4.5)").
So I have downloaded the latest stable release, cd to the folder, and wanted to run
./configure
make
make install
But the ./configure is failing due to "configure: error: Can not find libgomp !". There is no libgomp package I can install with brew (I already installed gcc which comes with gfortran and thought it would provide libgomp but apparently not).
Could someone help a poor applied mathematician trying to install a software on its macbook?
Thanks
EDIT
in my config.log I have this, maybe it will help:
configure:7433: checking for vim
configure:7449: found /usr/bin/vim
configure:7461: result: yes
configure:7507: checking for main in -lm
configure:7526: gcc -o conftest -g -O2 -O2 -fno-strict-overflow conftest.c -lm >&5
configure:7526: $? = 0
configure:7535: result: yes
configure:7548: checking for pthread_mutex_init in -lc
configure:7573: gcc -o conftest -g -O2 -O2 -fno-strict-overflow conftest.c -lc -lm >&5
configure:7573: $? = 0
configure:7582: result: yes
configure:7687: checking for omp_get_num_procs in -lgomp
configure:7712: gcc -o conftest -g -O2 -O2 -fno-strict-overflow conftest.c -lgomp -lc -lm >&5
ld: library not found for -lgomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
configure:7712: $? = 1
You don't say how you installed GCC. If you did it using homebrew, and
brew install gcc
you should be able to see:
/usr/local/Cellar/gcc/10.2.0/lib/gcc/10/libgomp.dylib
which would mean you need to compile with:
gcc ... -L /usr/local/Cellar/gcc/10.2.0/lib/gcc/10 -l gomp ...
Failing that, you can always search for libgomp with:
find /usr -name "lib*gomp*lib"
You can test which gcc you are using with:
type gcc

clang: error: unsupported option '-fopenmp'. Change default compiler?

I am on a Mac but I am not a Mac user. I need to run a make command (actually make makewisdom) to compile a software, but my compiler does not recognize some commands:
$ make makewisdom
gcc -I/Users/username/presto/include -I/opt/local/include/glib-2.0 -I/opt/local/lib/glib-2.0/include -I/opt/local/include -I/opt/local/include -I -I/opt/local/include -DUSEFFTW -DUSEMMAP -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -g -Wall -W -fPIC -O3 -ffast-math -Wno-unused-result -Wno-unused-but-set-variable -Wno-unused-but-set-parameter -fopenmp -o makewisdom makewisdom.c -L/opt/local/lib -lfftw3f
clang: error: unsupported option '-fopenmp'
clang: error: unsupported option '-fopenmp'
make: *** [makewisdom] Error 1
I know the problem is with clang. I am reading lots of discussions (such this and this, and more), and trying to workaround the problem, but no luck yet. It maybe that I just need to set a different default compiler? (to, for example, gcc, whatever is the version I have installed):
$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

GCC 4.9.2 issues - linking error "start" symbol undefined (after yosemite update)

So, I upgraded to Yosemite and also updated gcc to gcc-4.9. Now nothing works. Even a basic C program like this:
#include "stdio.h"
void main() {
printf("woh!");
}
The details of gcc -v a.c is below. The basic error is:
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Here is my configuration for CC, clang and gcc.
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/4.9.2/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../configure --build=x86_64-apple-darwin14.0.0 --prefix=/usr/local/Cellar/gcc/4.9.2 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-cloog=/usr/local/opt/cloog --with-isl=/usr/local/opt/isl --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --with-pkgversion='Homebrew gcc 4.9.2' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --enable-multilib
Thread model: posix
gcc version 4.9.2 (Homebrew gcc 4.9.2)
I had created links for gcc to gcc-4.9 which was installed successfully using Homebrew. I have run brew doctor and received no issues. Command Line Tools was updated using code-select -install
Gautams-MacBook-Pro:~ gautamrege$ ls -l /usr/local/bin/gcc
lrwxrwxr-x 1 gautamrege admin 22 Nov 14 20:14 /usr/local/bin/gcc# -> /usr/local/bin/gcc-4.9
Gautams-MacBook-Pro:~ gautamrege$ ls -l /usr/local/bin/gcc-4.9
lrwxrwxr-x 1 gautamrege admin 31 Nov 14 20:14 /usr/local/bin/gcc-4.9# -> ../Cellar/gcc/4.9.2/bin/gcc-4.9
These are my CC and clang versions (basically the same)
Gautams-MacBook-Pro:~ gautamrege$ CC -v
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
Gautams-MacBook-Pro:~ gautamrege$ clang -c
clang: error: no input files
Gautams-MacBook-Pro:~ gautamrege$ clang -v
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
Finally, here is the detailed log using gcc -v.
$ gcc -v a.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/4.9.2/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../configure --build=x86_64-apple-darwin14.0.0 --prefix=/usr/local/Cellar/gcc/4.9.2 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-cloog=/usr/local/opt/cloog --with-isl=/usr/local/opt/isl --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --with-pkgversion='Homebrew gcc 4.9.2' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --enable-multilib
Thread model: posix
gcc version 4.9.2 (Homebrew gcc 4.9.2)
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.10.0' '-v' '-mtune=core2'
/usr/local/Cellar/gcc/4.9.2/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/cc1 -quiet -v -D__DYNAMIC__ a.c -fPIC -quiet -dumpbase a.c -mmacosx-version-min=10.10.0 -mtune=core2 -auxbase a -version -o /var/folders/dr/kc0cps51533gqx5zm8gv33x80000gn/T//ccvx7Ydq.s
GNU C (Homebrew gcc 4.9.2) version 4.9.2 (x86_64-apple-darwin14.0.0)
compiled by GNU C version 4.9.2, GMP version 6.0.0, MPFR version 3.1.2-p10, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/Cellar/gcc/4.9.2/include"
ignoring nonexistent directory "/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../x86_64-apple-darwin14.0.0/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include
/usr/local/include
/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed
/usr/include
/System/Library/Frameworks
/Library/Frameworks
End of search list.
GNU C (Homebrew gcc 4.9.2) version 4.9.2 (x86_64-apple-darwin14.0.0)
compiled by GNU C version 4.9.2, GMP version 6.0.0, MPFR version 3.1.2-p10, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f90e36a64a568856fa161772b1b4076f
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.10.0' '-v' '-mtune=core2'
as -arch x86_64 -force_cpusubtype_ALL -o /var/folders/dr/kc0cps51533gqx5zm8gv33x80000gn/T//cckdbo1t.o /var/folders/dr/kc0cps51533gqx5zm8gv33x80000gn/T//ccvx7Ydq.s
COMPILER_PATH=/usr/local/Cellar/gcc/4.9.2/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2/libexec/gcc/x86_64-apple-darwin14.0.0/:/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/
LIBRARY_PATH=/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../:/usr/lib/
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.10.0' '-v' '-mtune=core2'
/usr/local/Cellar/gcc/4.9.2/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/collect2 -dynamic -arch x86_64 -macosx_version_min 10.10.0 -weak_reference_mismatches non-weak -o a.out -L/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2 -L/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../.. /var/folders/dr/kc0cps51533gqx5zm8gv33x80000gn/T//cckdbo1t.o -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
collect2 version 4.9.2
/usr/bin/ld -dynamic -arch x86_64 -macosx_version_min 10.10.0 -weak_reference_mismatches non-weak -o a.out -L/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2 -L/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../.. /var/folders/dr/kc0cps51533gqx5zm8gv33x80000gn/T//cckdbo1t.o -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
#(#)PROGRAM:ld PROJECT:ld64-136
configured to support archs: armv6 armv7 armv7s i386 x86_64
Library search paths:
/usr/local/Cellar/gcc/4.9.2/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2
/usr/local/Cellar/gcc/4.9.2/lib
/usr/lib
/usr/local/lib
Framework search paths:
/Library/Frameworks/
/System/Library/Frameworks/
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I'm pretty sure I am making some basic mistake.Needless to day, all other package installations are failing because of this same error. Any ideas?
Copying information from comments to this Community Wiki answer.
Tim Smith pointed out that the key to this problem is to run:
sudo xcode-select -s /Library/Developer/CommandLineTools/
This makes sure that clang invokes the XCode 6 version of clang.

Build shared library with Clang++

I am trying to build a shared library (dll for Windows) using Clang++.
I have run the following commands:
clang++ -c -o hello.o hello.cpp
clang++ -shared -v -o hello.dll hello.o
The first command works fine but when I try to build the dll I get this error:
clang version 3.2 (tags/RELEASE_32/final)
Target: i686-pc-mingw32
Thread model: posix
"c:/MinGW/bin/g++.exe" -shared -v -m32 -o worker.dll worker.o
Using built-in specs.
COLLECT_GCC=c:/MinGW/bin/g++.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.2 (GCC)
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/4.6.2/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../;/mingw/lib/
COLLECT_GCC_OPTIONS='-shared' '-v' '-m32' '-o' 'worker.dll' '-shared-libgcc' '-mtune=i386' '-march=i386'
c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/collect2.exe --shared -Bdynamic -e _DllMainCRTStartup#12 --enable-auto-image-base -u ___register_frame_info -u ___deregister_frame_info -o worker.dll c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../dllcrt2.o c:/mingw/bin/../lib/gcc/mingw32/4.6.2/crtbegin.o -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.2 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../.. -L/mingw/lib worker.o -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/4.6.2/crtend.o
Cannot export _hello: symbol not found
collect2: ld returned 1 exit status
clang++: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
Here is the file I am compiling:
// hello.cpp
#include <iostream>
#if defined(_WIN32)
#define LIBRARY_API __declspec(dllexport)
#else
#define LIBRARY_API
#endif
extern "C" void LIBRARY_API hello();
void hello()
{
std::cout << "Hello, World!" << std::endl;
}
A follow up to this important question is that as of clang++ version 6.0, the compilation of a DLL by clang is fine. There are differences for the name mangling of C++ functions in C++ for MSVC and CLang, that can impair the use of DLL's in mixed environments but these differences are easy to spot and correct.

How to build a multifile c++ project for llvm (lli) using clang++?

I have a multifile c++ project, built with help of Makefile. CC and CXX were gcc and g++ relatively. Now I need to try this project under llvm (lli).
I have replaced
CC = gcc
CXX = g++
LD = ld
with
CC = clang
CXX = clang++
LD = clang++
and added -emit-llvm to cflags:
CFLAGS_DEBUG = $(CFLAGS) -g -emit-llvm
...
CFLAGS_RELEASE = $(CFLAGS) -O2 -emit-llvm
Then I invoked make. Object files were built, but while collecting them into application, I got this error:
obj/Debug/../../XX/YY.o: file not recognized: File format not recognized
collect2: ld returned 1 exit status
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So, how I must build this project properly for the LLVM runtime interpreter (lli)?
You should link via llvm-link, not via clang++

Resources