How do I link libraries in Xcode 4? - c

I'm a complete beginner to Apple's Xcode, but I have followed the Xcode documentation and the advice of a few related questions without success.
I installed GMP to /usr/local/bin, wrote a short program using the library, and compiled with gcc main.c -lgmp. It compiled with no warnings or errors, and the executable worked flawlessly.
I started a new Xcode project (Command Line Tool; Type: C), copied the code to the newly created main.c, and opened the project build settings. From there I set Linking > Other Linker Flags to -lgmp and Search Paths > Library Search Paths to /usr/local/bin. However, the build fails with the preprocessor error "Gmp.h: No such file or directory".
I have tried almost every header imaginable:
#include "gmp.h"
#include <gmp.h>
#include "gmp"
#include "libgmp.a" . . .
This has been my main obstacle over the last three months which has prevented me from learning C. Any help leading me to an eventual solution would be greatly appreciated.

There's a few things you have to set up in your Xcode project. For example, I have gmp installed in /opt/gmp/5.0.2 and I will use that as an example. The actual library is installed into /opt/gmp/5.0.2/lib and the header files into /opt/gmp/5.0.2/include. When installing the library setting the --PREFIX flag to /opt/gmp/5.0.2 would handle this automatically. If you don't set this flag the prefix is usually set to /usr/local by default.
The Other Linker Flags looks right, it should be the name of the library.
Set the Header Search Path to the include directory, in my case /opt/gmp/5.0.2/include.
Set the Library Search Path to the lib directory, in my case /opt/gmp/5.0.2/lib.
Since the header search path has been set, you should now be able to include the header file like this:
#include <gmp.h>
Of course, replace /opt/gmp/5.0.2 with the PREFIX path you used when you installed gmp.
Lastly, you typically don't install libraries to /usr/local/bin, you would install to /usr/localand let any binaries be installed into bin while libraries like these would be installed into lib. Of course any path scheme would work, I usually recommend /opt/<project-name>/<version-number> since it allows me to keep better track of what I have installed and have multiple versions of the same libraries and tools without having to deal with collisions.

I have updated my system from snow leopard to mountain lion and had to install gmp.
First of all I have installed Xcode CommandLineTools set.
Secondly, installed Homebrew. Then with it I have done steps in this topic: https://apple.stackexchange.com/questions/38222/how-do-i-install-gcc-via-homebrew
In my last step, made changes to an xcode project as colleague Marcus Karlsson told.
It's finally working! Very big Thank You :)

Related

trying to use cuda with go on windows

There are various Go libraries that rely on the cuda.h file and the cuda library (specifically ML libraries). Every time I try to install one of these libraries on Windows, I get an error saying
fatal error: cuda.h: No such file or directory
//#include <cuda.h>
I am aware of what I need to do (link the Cuda library/header files to the go library that I am trying to install), however, I am not sure how to go about doing this especially on windows. I am using GCC and not MSVC for various reasons, but even when I've tried using MSVC, I've had the same issues.
Is there some way that I can link the cuda compiler/header files directly to my Go env or do I need to manually point the go/cgo compiler to the directory holding the Cuda headers and how do I go about doing this?
I've tried asking a few of the developers who make these libraries for help but most of them are linux users so they don't really know. An exhaustive google search has really lead me nowhere so I'm asking here.
I was able to find the answer.
The windows cuda installer installs things by default into a weird path:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include
This path caused a lot of pain for the compiler as a result of the spaces in the folder names. After reinstalling Cuda into C:\CUDA\v8.0 and then appending my CFLAGS and LDFLAGS appropriately in my CGO file I was able to get things to run correctly.
For reference here are the CFLAGS and LDFLAGS that I used to get this to work:
//#cgo windows LDFLAGS:-LC:/cuda/v8.0/lib/x64
//#cgo windows CFLAGS: -IC:/cuda/v8.0/include
import "C"
This was with the github.com/chewxy/cu go library. I also appended the new flags into that library in the cgoflags.go file because the maintainer did not have version 8 or version 9 in there already. I mentioned this to him and he might update it later but for now that's what you have to do.

What files can be safely deleted from the MinGW-w64 binaries?

I am using the MinGW-w64 installer found here:
https://mingw-w64.org/doku.php/download/mingw-builds
Installing to a directory creates a folder called mingw32 or mingw64 that contains the compiler. I already know that the additional batch script, HTML link, and uninstall executable in the same directory can be deleted.
The mingw32/64 folder contains these folders:
bin
etc
include
lib
libexec
licenses
opt
share
i686-w64-mingw32 (mingw32) or x86_64-w64-mingw32 (mingw64)
I only wish to compile with C and nothing else. So what files can I delete in order to reduce the file count because the compiler contains files for Ruby, Fortran and other languages which as I stated, I don't need it.
Why are people still using Mingw or Mingw64 instead of TDM-GCC ? You have to rely on the default builds or build it yourself from source. Trying to install it on your own terms sucks.
TDM-GCC on the other hand, you can specify where and which components to install as you wish. You can upgrade or remove it without any worries. It's more fast, easy,modern and equivelant these days.
Now for your question, you can download and install TDM-GCC by it's installer. I don't really think you need any further instructions as a 5 year old would understand how to use it. Just select only the components required for c.
If you don't understand,I would gladly help you more if needed. ^_^
If you want a smaller GCC toolset, try MinGW Distro.
Scroll down to "How To Build Your Own Distro", download components-14.1.7z, extract it, extract binutils 2.27 and GCC 6.3.0 inside the same folder. The total size is about 270MB.
as far as i know the minimum are yours
1, bin
5. libexec
not all tools in bin you may need but generally i would hold both of those dirs
you also ned libraries and headers if you want to build abainst standard c library and windows.h, so
include
lib
is also needed (hovever part of it may contain also headers and libraries for c++ standard library) all those main 3 libs are kinda mixed there afaik so it might be a bit hard to find what comes for what

"'portaudio.h' file not found" error in XCode 5.1

I've downloaded the portaudio codebase and compiled it fully with source, and installed it to my system with these commands:
./configure
make
sudo make install
But XCode is complaining to me, even when I put -lportaudio in the Other Linker Flags for the project settings.
I've researched this problem and tried whatever I could find on Stack Overflow, but there was no decisive answer that would work for me. Any advice on how to fix this?
I'm using an older version of XCode and haven't bothered looking at how the interface might have changed in the newer versions, but this is generally solved for me by modifying the User Search Paths under your project settings. Look at the screenshot, add /usr/local/include to Header Search Paths and make Always Search User Paths "Yes." That should do the trick
Edit:
One more thing to note, this is only /usr/local/include because that's the default install directory for the portaudio.h file in the portaudio build (as it is with many libraries).
If you have a different prefix other than /usr/local/include, add that instead.

stdio.h file not found when compiling c program

#include <stdio.h>
int main(void)
{
printf ("Programming is fun");
return 0;
}
When I use the cc command in terminal to compile the above program it returns
fatal error: 'stdio.h' file not found`
I compiled this exact code on ubuntu where it created an a.out-file, which sadly did not run either. While I'm not sure why either happens at least no error was produced on ubuntu.
For recent macOS, people would miss C headers after system update.
cd /Library/Developer/CommandLineTools/Packages
open macOS_SDK_headers_for_macOS_xx.pgk
I guess this is another Apple's bug.
You don't specify the platform you're on...
If stdio.h is not found, it usually mean you don't have the necessary header files on your system (usually in /usr/include/).
Your compiler might be working fine, but it looks like the C-Library headers are not installed.
Depending on your platform, you'll have to install them.
It's a bit weird that you have a working compiler without C library headers.
You may want to reinstall your compiler, or search for a package which contains the headers for the C library.
If you're on OS X, (re)install Xcode as well as the command line tools.
If you're on Linux, use your package manager to install the development package for C (e.g. libc6-dev on Debian).
Before you go reinstalling your compiler you might want to check your include paths. As you don't specify your platform I can only provide general information but this is similar on many platforms.
Using cc you need to set them in the environment. Try typing 'env' to see your current settings. There needs to be a line that sets a variable 'include' with the full path to the location of your studio.h.
You can also set the include path in the make file or, if using an IDE, in the project settings.
There is something wrong with how your compiler is configured.

assert.h missing when compiling with mingw

I am compiling C code with MinGW. The C code is a tcl package/extension.
(using the MinGW compilor, downloaded: mingw-get-inst-20111118.exe)
Compiling the code (e.g. the tcl package) works fine under linux.
I am running "./configure" and using the supplied "Makefile.in".
The problem is that the C code at some point includes "assert.h".
The other header files are fine, e.g. for "string.h" and "stdlib.h".
This is because the TCL sources include a subfolder called "./compat". In this filder the header files are located just in case they are not found somewhere else.
But in the "./compat" folder the file "assert.h" is missing. So I get an error.
I searched for the header file in "c:\MinGW\include" but I did not find "assert.h" there.
Either I copy my own "assert.h" in the "./compat" folder. Or I install some MinGW package that puts some "assert.h" in a subfolder of "c:\MinGW\".
=== SOLUTION: ====
c:\MinGW\include\assert.h
I was the hole time in front of me!!!
My bad! thx.
<assert.h> is part of standard C and included with the base dev package:
Did you download the dev package?
Did you download this?
http://sourceforge.net/projects/mingw/files/MinGW/Base/mingw-rt/mingwrt-3.20/
I don't know about the peculiarities of the tcl package, but if it includes its own assert.h. then you should include on on the Include path, along with the libraries that came with it.
Which IDE are your using?
It seems you downloaded only mingw partially. Download the full development package that is bundled with IDEs like Codelite and Code::Blocks.

Resources