gcc compiler in cygwin does not execute properly (gives up?) - c

The gcc compiler in cygwin does not execute properly (gives up?) from any folder but /bin. For instance, attempting to compile my code from /bin works, but from /bin/myprogram using the verbose option I get the following message from the compiler:
gcc version 4.5.3 (GCC)
COLLECT_GCC_OPTIONS='-O' '-ansi' '-v' '-c' '-mtune=generic' '-march=i686'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/cc1.exe -quiet -v -D__CYGWIN32__ -D__CYGWIN__ -D__unix__ -D__unix -idirafter /usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../include/w32api -idirafter /usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/lib/../../include/w32api sb.c -quiet -dumpbase sb.c -mtune=generic -march=i686 -ansi -auxbase sb -O -ansi -version -o /tmp/ccyhmnJp.s
Makefile:101: recipe for target `sb.o' failed
make: *** [sb.o] Error 1
Other people have reported similar problems:
http://cygwin.com/ml/cygwin/2010-01/msg00939.html
The solution appears to be to modify the path, although "remove X from the path" sounds like a temporary (and strange) workaround, not a permanent solution, and I don't know what to remove. Regardless I ran cygcheck from /bin and from /bin/myprogram (in which I want to compile). The output follows.
From /bin:
$ cygcheck /usr/lib/gcc/i686-pc-cygwin/4.5.3/cc1.exe
C:\cygwin\lib\gcc\i686-pc-cygwin\4.5.3\cc1.exe
C:\cygwin\bin\cygcloog-0.dll
C:\cygwin\bin\cygwin1.dll
C:\WINDOWS\system32\KERNEL32.dll
C:\WINDOWS\system32\ntdll.dll
C:\cygwin\bin\cyggmp-10.dll
C:\cygwin\bin\cygppl_c-4.dll
C:\cygwin\bin\cygppl-9.dll
C:\cygwin\bin\cyggcc_s-1.dll
C:\cygwin\bin\cygstdc++-6.dll
C:\cygwin\bin\cyggmpxx-4.dll
C:\cygwin\bin\cygpwl-5.dll
C:\cygwin\bin\cyggmp-3.dll
C:\cygwin\bin\cygiconv-2.dll
C:\cygwin\bin\cygintl-8.dll
C:\cygwin\bin\cygmpc-1.dll
C:\cygwin\bin\cygmpfr-1.dll
C:\cygwin\bin\cygmpfr-4.dll
C:\cygwin\bin\cygppl_c-2.dll
C:\cygwin\bin\cygppl-7.dll
C:\cygwin\bin\cygz.dll
From /bin/myprogram:
$ cygcheck /usr/lib/gcc/i686-pc-cygwin/4.5.3/cc1.exe
C:\cygwin\lib\gcc\i686-pc-cygwin\4.5.3\cc1.exe
C:\cygwin\bin\cygcloog-0.dll
C:\cygwin\usr\local\bin\cygwin1.dll
C:\WINDOWS\system32\ADVAPI32.DLL
C:\WINDOWS\system32\KERNEL32.dll
C:\WINDOWS\system32\ntdll.dll
C:\WINDOWS\system32\RPCRT4.dll
C:\WINDOWS\system32\Secur32.dll
C:\cygwin\bin\cyggmp-10.dll
C:\cygwin\bin\cygppl_c-4.dll
C:\cygwin\bin\cygppl-9.dll
C:\cygwin\bin\cyggcc_s-1.dll
C:\cygwin\bin\cygstdc++-6.dll
C:\cygwin\bin\cyggmpxx-4.dll
C:\cygwin\bin\cygpwl-5.dll
C:\cygwin\bin\cyggmp-3.dll
C:\cygwin\bin\cygiconv-2.dll
C:\cygwin\bin\cygintl-8.dll
C:\cygwin\bin\cygmpc-1.dll
C:\cygwin\bin\cygmpfr-1.dll
C:\cygwin\bin\cygmpfr-4.dll
C:\cygwin\bin\cygppl_c-2.dll
C:\cygwin\bin\cygppl-7.dll
C:\cygwin\usr\local\bin\cygz.dll
There are some clear differences but I am not sure what these mean.
Path on my system returns:
$ $PATH
-bash: /usr/local/bin:/usr/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS: /cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/ProgramFiles/ImageMagick-6.8.6-Q16 :/cygdrive/c/Ruby193/bin:/cygdrive/c/Program: No such file or directory
Question is, what do I do now? If the answer is change the path variable, what should be removed? Is the path variable stored in one of the bash resource files or do changes have to be made from the command line? Many thanks!

You have various options. The problem is the wrong /usr/local/bin/cygz.dll
Either delete your rogue /usr/local/bin/cygz.dll (preferred),
or fix you PATH
$ export PATH=/usr/bin:/usr/local/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS
Explanation:
On windows the library search path has . before $PATH. So when you are in /bin the right cygz.dll will be found. Otherwise the broken one from your path will be found first.

Related

Adding a step for building linker script file from template in CMake?

Basically, before linking is performed, I would like to convert a GCC linker script template file into a final linker script, as discussed in Can I use Preprocessor Directives in .ld file - and I'd like that step to be performed by Cmake.
I guess my problem is similar to the discussion in add_custom_command is not generating a target ; but I still cannot see how to solve it. Here is a minimal example, where I'm faking with an empty .ld linker script template file; first, let's create the files using bash:
mkdir /tmp/cmake_test && cd /tmp/cmake_test
touch my_linkerscript_template.ld
cat > main.c <<'EOF'
#include <stdio.h>
const char greeting[] = "hello world";
int main() {
printf("%s!\n", greeting);
return 0;
}
EOF
cat > CMakeLists.txt <<'EOF'
cmake_minimum_required(VERSION 3.13)
SET(CMAKE_INCLUDE_CURRENT_DIR ON)
project(foobar C)
message("PROJECT_NAME is '${PROJECT_NAME}'")
add_executable(${PROJECT_NAME}
main.c
)
add_compile_options(-Wall
)
target_link_options(${PROJECT_NAME} PRIVATE
"LINKER:--script=${CMAKE_SOURCE_DIR}/my_linkerscript.ld"
)
add_custom_command(OUTPUT ${CMAKE_SOURCE_DIR}/my_linkerscript.ld
DEPENDS ${CMAKE_SOURCE_DIR}/my_linkerscript_template.ld
COMMAND bash ARGS -c "gcc -E -x c -CC -I/usr/include my_linkerscript_template.ld | grep -v '^#' > my_linkerscript.ld"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "build my_linkerscript_template.ld into my_linkerscript.ld (${CMAKE_CURRENT_SOURCE_DIR})"
VERBATIM
)
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_DEPENDS "${CMAKE_SOURCE_DIR}/my_linkerscript.ld")
EOF
Note that my_linkerscript.ld is not created above; as I'd intend to put my_linkerscript_template.ld in git, and then have the final my_linkerscript.ld generated on demand.
If in this case, I do:
mkdir /tmp/cmake_test/build && cd /tmp/cmake_test/build
cmake ../ -DCMAKE_BUILD_TYPE=Debug
make
I get:
$ make
make[2]: *** No rule to make target 'D:/msys64/tmp/cmake_test/my_linkerscript.ld', needed by 'foobar.exe'. Stop.
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/foobar.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
$ grep -r linkerscript .
./CMakeFiles/foobar.dir/build.make:foobar.exe: D:/msys64/tmp/cmake_test/my_linkerscript.ld
./CMakeFiles/foobar.dir/build.make: /D/msys64/mingw64/bin/cc.exe -g -Wl,--script=D:/msys64/tmp/cmake_test/my_linkerscript.ld -Wl,--whole-archive CMakeFiles/foobar.dir/objects.a -Wl,--no-whole-archive -o foobar.exe -Wl,--out-implib,libfoobar.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
So, I did get a dependency on my_linkerscript.ld; and it did get added to linker options - but there is no build step generated by the add_custom_command.
How can I have generate the proper dependencies, so that when my_linkerscript.ld is needed for linking but it does not exist, then commands (that are given in add_custom_command above) are run, to generate this file from the template?
Output of add_custom_command needs to be "consumed" by some target, otherwise the custom command has no effect.
The property LINK_DEPENDS does NOT provide a consumer, so you need to create consumer target by yourself:
# ... in addition to your code
# Create a consumer target for the linker script.
# So previous `add_custom_command` will have an effect.
add_custom_target(my_linkerscript
DEPENDS ${CMAKE_SOURCE_DIR}/my_linkerscript.ld)
# Make executable to depend on that target.
# So, the check whether to relink the executable will be performed
# after possible rebuilding the linker script.
add_dependencies(${PROJECT_NAME} my_linkerscript)

Can't compile a C program on a Mac after upgrading to Catalina 10.15

There's a previous question Can't compile C program on a Mac after upgrade to Mojave, and the answers to that have covered most of the variations on what goes wrong.
Now — as of Monday 2019-10-07 — you can upgrade to macOS Catalina 10.15. Once again, during the upgrade, the /usr/include directory has been blown away by the update, even though XCode 11.0 was installed before upgrading (from Mojave 10.14.6) to Catalina. Consequently, compilers built to expect that there is a /usr/include directory do not work any longer.
The main recommended step for the Mojave issues — using the command:
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
does not work out of the gate because the directory /Library/Developer/CommandLineTools/Packages/ does not exist (so there's not yet a .pkg file to open).
Is there a good (official) way to create and populate the directory /usr/include?
Before you proceed, make sure to install xcode command line tools.
xcode-select --install
Actually, you can do it! Actually all the C headers are found here in this folder:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/
We just need to create symlink for all the headers file into this folder:
/usr/local/include/
It worked for me! the following command line will take care of all the problems:
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
You will get some warning. Some of the headers already exists, like this:
ln: /usr/local/include//tcl.h: File exists
ln: /usr/local/include//tclDecls.h: File exists
ln: /usr/local/include//tclPlatDecls.h: File exists
ln: /usr/local/include//tclTomMath.h: File exists
ln: /usr/local/include//tclTomMathDecls.h: File exists
ln: /usr/local/include//tk.h: File exists
ln: /usr/local/include//tkDecls.h: File exists
ln: /usr/local/include//tkPlatDecls.h: File exists
totally ok to ignore. that's all.
For me adding the following path to CPATH solved the issue:
export CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
TL;DR
It appears that Apple considers /usr/include as something that has gone the way of the dodo — it is extinct — or maybe it's like Monty Python's Parrot.
Using the Apple-provided GCC (actually, that's Clang by any other name, as the version information shows) or Clang avoids problems. Both /usr/bin/gcc and /usr/bin/clang will find the system libraries four directory levels below:
/Applications/Xcode.app/Contents/Developer/Platforms/…
If you build your own GCC or other compiler, you will (probably) need to configure it to find the system libraries under the Xcode application directory.
Explorations
Immediately after the upgrade, I ran XCode 11.0. It wanted to install some extra components, so I let it do so. However, that did not reinstate /usr/include or the directory under /Library.
One of the other bits of advice in the previous question was to run:
xcode-select --install
When doing so, it claimed that it downloaded the command line utilities, and it ensured that /usr/bin/gcc and /usr/bin/clang etc were present. That's a useful step (though I didn't definitively check whether they were present before).
$ /usr/bin/gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$
Using /usr/bin/gcc, it is now possible to compile programs:
$ make CC=/usr/bin/gcc al
co RCS/al.c,v al.c
RCS/al.c,v --> al.c
revision 1.7
done
/usr/bin/gcc -I/Users/jleffler/inc -g -O3 -std=c11 -pedantic -Wall -Wextra -Werror -Wshadow -Wmissing-prototypes -Wpointer-arith -Wold-style-definition -Wcast-qual -Wstrict-prototypes -DHAVE_MEMMEM -DHAVE_STRNDUP -DHAVE_STRNLEN -DHAVE_GETDELIM -o al al.c -L/Users/jleffler/lib/64 -ljl
$
However, /usr/include is still missing. There is a directory under /Library now:
$ ls /Library/Developer
CommandLineTools PrivateFrameworks
$ ls /Library/Developer/CommandLineTools
Library SDKs usr
$ ls /Library/Developer/CommandLineTools/SDKs
MacOSX.sdk MacOSX10.14.sdk MacOSX10.15.sdk
$ ls /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/
Entitlements.plist SDKSettings.json System
Library SDKSettings.plist usr
$
Neither the System nor the Library directory contain anything very promising.
When all else fails, read the manual
Next step — find and read the release notes:
Xcode 11 Release Notes
macOS Catalina 10.15 Release Notes
There's no information in there that relates to this. So, the probability is (AFAICS, after only an hour or two's effort) that Apple no longer support /usr/include — though it does still have a fully-loaded /usr/lib (no /lib though).
Time to check another compilation with GCC option -v added (in the makefile I used, setting UFLAGS adds the option to C compiler command line):
$ make UFLAGS=-v CC=/usr/bin/gcc ww
co RCS/ww.c,v ww.c
RCS/ww.c,v --> ww.c
revision 4.9
done
/usr/bin/gcc -I/Users/jleffler/inc -g -O3 -std=c11 -pedantic -Wall -Wextra -Werror -Wshadow -Wmissing-prototypes -Wpointer-arith -Wold-style-definition -Wcast-qual -Wstrict-prototypes -DHAVE_MEMMEM -DHAVE_STRNDUP -DHAVE_STRNLEN -DHAVE_GETDELIM -v -o ww ww.c -L/Users/jleffler/lib/64 -ljl
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ww.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15 -target-cpu penryn -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 512.4 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I /Users/jleffler/inc -D HAVE_MEMMEM -D HAVE_STRNDUP -D HAVE_STRNLEN -D HAVE_GETDELIM -I/usr/local/include -O3 -Wall -Wextra -Werror -Wshadow -Wmissing-prototypes -Wpointer-arith -Wold-style-definition -Wcast-qual -Wstrict-prototypes -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -pedantic -std=c11 -fdebug-compilation-dir /Users/jleffler/src/cmd -ferror-limit 19 -fmessage-length 110 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.15.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/77/zx9nk6dn7_dg4xd4stvt42v00000gn/T/ww-4cb85b.o -x c ww.c
clang -cc1 version 11.0.0 (clang-1100.0.33.8) default target x86_64-apple-darwin19.0.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/Users/jleffler/inc
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.15.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ww -L/Users/jleffler/lib/64 /var/folders/77/zx9nk6dn7_dg4xd4stvt42v00000gn/T/ww-4cb85b.o -ljl -L/usr/local/lib -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/lib/darwin/libclang_rt.osx.a
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/dsymutil" -o ww.dSYM ww
$
The key information in that blizzard of data is:
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
That's effectively the 'root' directory for the compilation, so there should be sub-directories under that for usr and usr/include:
$ ls /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
Entitlements.plist SDKSettings.json System
Library SDKSettings.plist usr
$ ls /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr
bin include lib libexec share
$ ls /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
AppleTextureEncoder.h dns_util.h memory.h simd
AssertMacros.h dtrace.h menu.h slapi-plugin.h
Availability.h editline miscfs spawn.h
AvailabilityInternal.h err.h module.modulemap sqlite3.h
AvailabilityMacros.h errno.h monetary.h sqlite3ext.h
AvailabilityVersions.h eti.h monitor.h stab.h
…lots more lines…
dirent.h mach-o security xcselect.h
disktab.h mach_debug semaphore.h xlocale
dispatch machine servers xlocale.h
dlfcn.h malloc setjmp.h xpc
dns.h math.h sgtty.h zconf.h
dns_sd.h membership.h signal.h zlib.h
$
This shows that the mile-long and totally unmemorable directory name does contain the standard C and POSIX headers, plus Apple-specific extras.
The previous /usr/local/ directory appears to be intact; the warning about usr/local/include not existing under the -isysrootdir is harmless (and not visible without the -v option).
Set the following implicit Make variables to point to where the headers are now located for Xcode Command Line Tools (Xcode CLI):
export CFLAGS+=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CCFLAGS+=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CXXFLAGS+=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CPPFLAGS+=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
The -isysroot option updates the location of the root files away from the system root directory /.
So, this ensures that the common /usr/* files are found in their new place.
That is, the files at /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk are now found. These files are:
Entitlements.plist
Library
SDKSettings.json
SDKSettings.plist
System
usr
On MacOS Catalina 10.15.4, with Xcode Version 11.5 (11E608c) I also needed to update the library path in my .zshrc (the MacOSX.sdk paths are new):
export CPATH='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include:/opt/local/include'
export LIBRARY_PATH='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib:/opt/local/lib'
I am a newbie with C++ compiler for R in OSX and I got the same issue that C++ could not find the header after OS was updated (missing math.h although it was there). I followed instructions from https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/ but nothing changed.
Finally, it worked for me after I reinstalled the Xcode CLI
xcode-select --install
and then change the flags to Var as #Coatless suggested:
export CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
If you have both the command line tools and XCode installed, make sure the SDK installed by the command line tools is actually being used:
#Check the current sdk
$ xcrun --show-sdk-path
#Change sdk
$ sudo xcode-select -s /Library/Developer/CommandLineTools #Using CommandLineTools SDK
$ sudo xcode-select -s /Applications/Xcode.app/Contents/Developer #Using XCode.app SDK
kudos to https://stackoverflow.com/a/61526989/596599 for this answer.
For me, it works well as follow:
1. xcode-select --install
2. sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
3. export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
In my case I seemed to have llvm and gcc also installed using homebrew. When I removed those, and thus relied fully on the macOS clang, it could find the headers and the compiling worked again.
Short Answer
/Library/Developer/CommandLineTools/usr/bin/clang++ -o main main.cpp -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk
The Result
Explanation
In current macOS version, the c/c++ headers are searched inside /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/, instead of /usr/include. So you need to 'reset' the root directory using -isysroot option.
Hope this makes sense 🙂.
If using an external LLVM installation, add these to your ~/.bash_profile
LLVM_PATH="/usr/local/opt/llvm/" # or any other path
LLVM_VERSION="11.0.0"
export PATH="$LLVM_PATH:$PATH"
export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
export LD_LIBRARY_PATH="$LLVM_PATH/lib/:$LD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH="$LLVM_PATH/lib/:$DYLD_LIBRARY_PATH"
export CPATH="$LLVM_PATH/lib/clang/$LLVM_VERSION/include/"
export LDFLAGS="-L$LLVM_PATH/lib"
export CPPFLAGS="-I$LLVM_PATH/include"
export CC="$LLVM_PATH/bin/clang"
export CXX="$LLVM_PATH/bin/clang++"
(adjust the clang version and the external llvm installation path.)
Then run source ~/.bash_profile
apue.h dependency was still missing in my /usr/local/include after following Komol Nath Roy answer in this question.
I downloaded the dependency manually from git and placed it in /usr/local/include
The solution was simpler than I thought. Install clang/llvm.
brew install llvm
Then we need to create symlinks ourselves.
for f in /usr/local/Cellar/llvm/9.0.0_1/bin/clang*; do ln -s ${f} /usr/local/bin/"${f##*/}"; done
And
ln -s /usr/local/Cellar/llvm/9.0.0_1/include/c++ /usr/local/include/c++
Depending upon your llvm version, modify the above commands.
Now, you can compile C++ programs without passing any custom flags.
clang++ hello.cpp
I tried 1) manually linking 2) brew install llvm, but they did not work.
Finally, this worked for me:
https://gitmemory.com/issue/pytorch/pytorch/31190/565153503
By setting the following env vars:
export CC=clang
export CXX=clang++
export MACOSX_DEPLOYMENT_TARGET=10.9
In my case, i did a millions things but i reckon following steps helped fix ruby installation.
xcode-select --install
Set these flags
export CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
export CPPFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
rbenv install 2.6.3 -v
For me the error was: xcrun[20873:1179298] Failed to open macho file at /Library/Developer/CommandLineTools/usr/bin/clang++ for reading: Too many levels of symbolic links
So I opened my terminal and went to the following folder (as mentioned by the error message): /Library/Developer/CommandLineTools/usr/bin/
Then I deleted the shortcut file named clang++
sudo rm clang++
Next, I made a copy of executable file named clang and renamed the copied file to clang++
sudo cp clang clang++
And finally it works.

Gold linker ld.gold -plugin : unknown option

I'm trying to build Google's ligjingle following the Getting started steps, and I've reached the "Building" section.
When I issue either
ninja -C out/Debug
or
ninja -C out/Release
I get the following error :
I posted the output in pastebin too, if you prefer
ninja -C out/Debug
ninja: Entering directory `out/Debug'
[3/2606] LINK genmacro
FAILED: cc -Wl,-z,now -Wl,-z,relro -Wl,--fatal-warnings -pthread -Wl,-z,noexecstack -fPIC -B/home/nschoe/workspace/webrtc/jingle/trunk/third_party/binutils/Linux_x64/Release/bin -Wl,--disable-new-dtags -m64 -Wl,--icf=none -fuse-ld=gold -Wl,--gdb-index -o genmacro -Wl,--start-group obj/third_party/yasm/source/patched-yasm/tools/genmacro/genmacro.genmacro.o -Wl,--end-group
/home/nschoe/workspace/webrtc/jingle/trunk/third_party/binutils/Linux_x64/Release/bin/ld.gold: -plugin: unknown option
/home/nschoe/workspace/webrtc/jingle/trunk/third_party/binutils/Linux_x64/Release/bin/ld.gold: use the --help option for usage information
collect2: error: ld returned 1 exit status
[3/2606] CC obj/net/third_party/nss/ssl/libssl.sslauth.o
ninja: build stopped: subcommand failed.
I tried
ld.gold --help | grep "plugin"
and got :
--plugin PLUGIN Load a plugin library
--plugin-opt OPTION Pass an option to the plugin
So I suppose the error I'm getting is because somewhere in the code, ld.gold is called with -plugin xxx rather than --plugin xxx
I have been "playing" with grep -Hr and different combinations of "plugin" to try to find the problem, but so far I haven't found anything. I suppose it is hidden somewhere in a Makefile.
The problem is not that the -plugin option should be --plugin. ld.gold
accepts both options if it accepts either of them.
But it only accepts either of them if the build of binutils has been
configured with --enable-plugins. Documentation.
When you run ld.gold --help | grep "plugin" the output shows that --plugin is
a recognized option.
Therefore the problem appears to be this:-
/home/nschoe/workspace/webrtc/jingle/trunk/third_party/binutils/Linux_x64/Release/bin/ld.gold
has not been configured with --enable-plugins
When you run ld.gold --help | grep "plugin" you are executing the first ld.gold
that is found on your PATH. It is probably /usr/bin/ld.gold from your distro. You
can find out by running:
which ld.gold
Anyhow, it isn't
/home/nschoe/workspace/webrtc/jingle/trunk/third_party/binutils/Linux_x64/Release/bin/ld.gold
and it is an ld.gold that has been configured with --enable-plugins
If you cd into /home/nschoe/workspace/webrtc/jingle/trunk/third_party/binutils/Linux_x64/Release/bin/
and run:
./ld.gold -plugin
you will get:
./ld.gold: -plugin: unknown option
To fix the problem the ideal solution is to rebuild /home/nschoe/workspace/webrtc/jingle/trunk/third_party/binutils
as per that Documentation
If you cannot rebuild these third party binutils from source then it will
probably work if you just copy the system ld.gold detected by which over the one in the
third party binutils, or delete/rename the third party one and replace it with a symlink
to the system one. There is an outside chance that either of the these hacks
would cause you some obscure breakage.

make error: Building 64-bit GSL in Cygwin

Continuing from here, I am trying to build 64-bit GSL using GCC in Cygwin.
The call to ./configure (CC=x86_64-w64-mingw32-gcc CFLAGS=-m64 ./configure) goes through fine, but the call to make install results, after a whole load of folders are successfully processed, in
./.libs/libgslsiman.a: could not read symbols: Archive has no index; run ranlib to add one
collect2: ld returned 1 exit status
Makefile:326: recipe for target `siman_tsp.exe' failed
The full call that caused this was
Making all in siman
make2: Entering directory `/cygdrive/f/programming/c/libraries/gslCompiled/gsl-1.15/siman'
/bin/sh ../libtool --tag=CC --mode=link x86_64-w64-mingw32-gcc -m64 -o siman_tsp.exe siman_tsp.o libgslsiman.la ../rng/libgslrng.la ../ieee-utils/libgslieeeutils.la ../err/libgslerr.la ../sys/libgslsys.la ../utils/libutils.la -lm
libtool: link: x86_64-w64-mingw32-gcc -m64 -o .libs/siman_tsp.exe siman_tsp.o ./.libs/libgslsiman.a ../rng/.libs/libgslrng.a ../ieee-utils/.libs/libgslieeeutils.a ../err/.libs/libgslerr.a ../sys/.libs/libgslsys.a ../utils/.libs/libutils.a
Following advice here, I decided to run a ranlib in the ./siman/.libs directory on the libgslsiman.a file. Since that didn't work, I also tried to pack it myself using a call to ar -t libgslsiman.a.
However, this results in an identical error.
You manually forced use of the cross compiler. However, the rest of the build toolchain will still default to the 32-bit Cygwin versions instead of the 64-bit MinGW ones.
Instead of setting CC=..., pass --host x86_64-w64-mingw32 to ./configure to specify the host environment (ie where the library is going to be used).

Clang link-time optimization doesn't work properly on Fedora 18

I'm a newcomer to clang, so it's likely I'm doing something silly. But I've spent several hours looking for solutions, including searching here, where I haven't found questions addressing -flto with distro-provided packages. The detail of this description are specific to Fedora 18, but I'm having similar problems on Ubuntu 13.04, so the problem isn't specific to Fedora. It's either me or clang.
Problem: I'm trying to compile a simple hello-world program using clang++ -flto to get the benefits of link-time-optimization. Without -flto it works fine. With -flto it fails to link. Invoking as clang -flto -o hello hello.o -v to see the full linker command line, I get:
$ clang++ -flto -o hello hello.o -v
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-redhat-linux-gnu
Thread model: posix
"/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../.. -L/lib -L/usr/lib -plugin /usr/bin/../lib/LLVMgold.so hello.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crtn.o
/usr/bin/ld: /usr/bin/../lib/LLVMgold.so: error loading plugin
/usr/bin/ld: /usr/bin/../lib/LLVMgold.so: error in plugin cleanup (ignored)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
There seem to be two problems:
clang++ invokes the linker as /usr/bin/ld, and that's not the gold linker. Fedora18 installs gold as /usr/bin/ld.gold. I've tried creating a symlink from /usr/local/bin/ld to /usr/bin/ld.gold, verified that which ld says /usr/local/bin/ld, but clang++ doesn't use that. It seems to be hardwired to /usr/bin/ld.
clang++ invoked the linker with -plugin /usr/bin/../lib/LLVMgold.so. That's wrong, as the Fedora distribution of clang places it at /usr/lib64/llvm/LLVMgold.so.
I have tried manually invoking that linker line above with the following tweaks:
Replace -plugin /usr/bin/../lib/LLVMgold.so with -plugin /usr/lib64/llvm/LLVMgold.so. This yields the error message hello.o: file not recognized: File format not recognized. So the non-gold linker seems to know about plugins but wont take the .o's which contain LLVM bitcode.
Replace /usr/bin/ld with /usr/bin/ld.gold. This works, generates an executable that runs as expected.
Both of the above with --plugin instead of -plugin. This change makes no difference.
So what's the best way for somebody who prefers to stick to the system-provided packages to use clang -flto? I'm hoping there is a config file, or undocumented options or environment variables that will let me override these. Or better, that I'm missing a package and a "yum install ..." will fix it.
I would prefer not to invoke the linker directly, as then my makefiles need to know system objects and libraries that they should be ignorant of (e.g. crt1.o, crtbegin.o, crtend.o). I could also build clang myself, but I'm not seeing anything in its configure script that lets me configure the path of the linker and plugin.
I'm running Fedora 18. The only non-distro packages on the computer are google chrome and VMware Tools (it's a guest inside VMWare Fusion). Versions of relevant Fedora packages (the whole computer is "yum updated" as of today, 29-Apr-2013):
$ yum list --noplugins installed binutils* clang* llvm* gcc*
Installed Packages
binutils.x86_64 2.23.51.0.1-6.fc18 #updates
binutils-devel.x86_64 2.23.51.0.1-6.fc18 #updates
clang.x86_64 3.2-2.fc18 #updates
clang-devel.x86_64 3.2-2.fc18 #updates
clang-doc.noarch 3.2-2.fc18 #updates
gcc.x86_64 4.7.2-8.fc18 #fedora
gcc-c++.x86_64 4.7.2-8.fc18 #fedora
llvm.x86_64 3.2-2.fc18 #updates
llvm-libs.x86_64 3.2-2.fc18 #updates
There is an utility alternatives in Fedora - it allows to subtitute one linker with another on system level:
$ sudo alternatives --display ld
ld - status is auto.
link currently points to /usr/bin/ld.bfd
/usr/bin/ld.bfd - priority 50
/usr/bin/ld.gold - priority 30
Current `best' version is /usr/bin/ld.bfd.
$ sudo alternatives --set ld /usr/bin/ld.gold
About LLVMgold.so location you can only report a bug in Fedora Bugzilla, since the path is built-in in clang sources:
lib/Driver/Tools.cpp: std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
Fedora guys may apply a patch to the Clang's source package, or create a symlink to LLVMgold.so.
There is no changes even in Fedora 20 yet.

Resources