makefile works with cygwin on windows but not ubuntu - c

I have the following folder on my git repo - https://github.com/ryu577/base/tree/master/numerical/c/NumericalRecipiesCode/lib
On my windows machine which has cygwin installed, I can run make in that directory, which triggers the command -
gcc -o ../bin/lib/tst_libfns ../obj/nrutil.o ../obj/fileio.o ../obj/tst_libfns.o -I ../include -lm
This puts the tst_libfns.o and fileio.o in the obj directory.
Now, I pulled this repository into my ubuntu machine and tried the same thing. However, when the same command is generated there, it gives me the following error:
gcc -o ../bin/lib/tst_libfns ../obj/nrutil.o ../obj/fileio.o ../obj/tst_libfns.o -I../include -lm
../obj/fileio.o:fileio.c:(.text+0x52): undefined reference to `__getreent'
../obj/tst_libfns.o:tst_libfns.c:(.text+0x10): undefined reference to `__main'
collect2: error: ld returned 1 exit status
make: *** [../bin/lib/tst_libfns] Error 1
Am I missing something obvious?

I just figured it out. I had pulled in the .o files through git in the obj directory and gcc was somehow trying to use the existing files (which had been generated in Cygwin + Windows). Somehow, those .o files don't seem to be compatible with linux. When I delete them and run the make command again, the .o files are re-generated. I guess the moral of the story here is that binaries and executables generated by GCC in Windows are incompatible with Linux (and I'll guess vice versa).

Related

How to fix "cannot find -lz"

I am working on code have Zlib.h header, This header is found in my code folder, I compile this code by using
gcc -o x xx.c -lz
but I get on this
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
This happen just with Linux that I installed in a VBox.
How to fix that.
Try installing 'zlib1g-dev'. On Ubuntu this following command will install the library.
sudo apt install zlib1g-dev
When you type gcc foo.c, you ask gcc to compile and link the given file.
1. Compilation
Compilation consist of transforming the source file into an object file.
This step need the included files, like zlib.h to be found by gcc.
This step seems to be correct on system.
NB: You can ask gcc to only do this step typing gcc -c foo.c, or better gcc -Wall -c foo.c
2. Link
Once the object files have be created, then need to be linked to create an executable file.
It's that step that failed for you: your linked can't find the already compiled functions needed by your code.
When linking with option -lz, you tell your linker "search for libz.so file to find the missing functions"
On current linux distribution, you can install package like libz-dev to install the .so file in well known places. (/lib, /usr/lib, /usr/local/lib...)
If you don't have the libz.so file installed on the library search path, you can specify where is the library to your linker.
For instance, if libz.so is if /bar/baz directory, you can type gcc foo.c /bar/baz/libz.so. The same for libz.a.
In any case, you'll need the libz.so file or at least the libz.a file
See also What's the difference between .so, .la and .a library files?

Not able to link gcc against libbluetooth library

I am using BlueZ library for developing bluetooth based application for linux using C. I am trying to link libbluetooth-dev to my C file but it is not working.
/usr/bin/ld: cannot find -libbluetooth-dev
collect2: error: ld returned 1 exit status
My project directory is bluez-5.45, i am trying to build the project from this folder and added my .C file in this main folder
I installed libbluetooth-dev library and tryed that linking but not able to do that.
Command for linking:
gcc -o output myfile.c -libbluetooth
please let me know the mistake done by me.
The "lib" is implied, so link against "bluetooth" instead of "libbluetooth":
$ gcc -o output myfile.c -lbluetooth

Gcc collect2: fatal error: cannot find 'ld'

I'm going through the tutorial on making an OS on http://wiki.osdev.org/Bare_Bones. When I try to link boot.o and kernel.o using this command: i686-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc , I just get this error:
collect2: fatal error: cannot find 'ld'
compilation terminated.
I just installed fresh Ubuntu 15.10 that with gcc-5.2.1 and binutils-2.25.1 .
I have searched the internet for answers but nothing helped.
I also got once the same error during a pentest while I was trying to compile my exploit on the victim server.
In my case, the directory where the "ld" program was located had not been defined in the PATH environment variable, so I simply added it.
eg. export PATH=$PATH:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin
I had this error while hacking a remote machine and trying to use gcc to compile an exploit on the victim machine.
I simply copied the program ld to /tmp/, the working directory where i was compiling my exploit exploit.c by running
cp /usr/bin/ld /tmp/ld
followed by the original gcc compile command and the compile worked.
I searched a lot to fix this issue and nothing worked but lastly i uninstalled MinGw and reinstalled it then did edited the environment variables again and then uninstalled and reinstalled vs code extensions then it worked.

Install third-party C library on Mac OS X

I'd like to install a third-party C library (http://cgm.cs.mcgill.ca/~avis/C/lrs.html) on a Mac OS X. However, the binaries won't seem to install on a Mac OS X (10.9.5). The library is intended for Unix/Linux platforms.
Here are a couple example of errors I get when trying to install the make file. First, here's the error when running make all out of the box (for some reason, running make all64 does nothing):
ld: library not found for -lgmp
I installed the GMP library (https://gmplib.org/) via MacPorts in /opt/local. However, the library does not appear to be found:
cc 2nash-GMP.o -L. -llrsgmp -L/opt/local/include -lgmp -o 2nash
ld: library not found for -lgmp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [2nash] Error 1
rm 2nash-GMP.o
How can I get around all this and install on a Mac?
I'll mention that I intend to call a function from this C library many, many times within functions from some (Matlab) code I've written. I'd prefer any potential solution to allow for this.
Update #1:
I've since done the following:
In the makefile, changed LIBDIR from /usr/lib to /opt/local/lib
In the makefile, changed INCLUDEDIR from /usr/include to /opt/local/include
Copied gmp.h file from /opt/local/include to /usr/include
In the makefile, changed RANLIB ?= /bin/true to RANLIB ?= /usr/bin/true
Now, when I run make all, I get the following message:
make: Nothing to be done for `all'.
What other steps should be taken?
I think you would, instead, want something like:
cc 2nash-GMP.o -L. -llrsgmp -I/opt/local/include -L/opt/local/lib -lgmp -o 2nash
The -I option specifies a path to headers to include. The -L option specifies a path to library files to include.
Change the variable LIBDIR in the makefile to the location where the libraries are installed, e.g.:
LIBDIR = /opt/local/lib

how do i use cmockery in my projects

I was searching for a way to create mocking objects with c-code until I stumbled upon cmockery.
For me it seems to be the best mocking software available since it doesn't have a lot of dependencies.
I'm working in ubuntu and downloaded the tarball cmockery from https://code.google.com/p/cmockery/downloads/list
I ran the ./configure, make and make install.
I am able to execute the given examples but I just can't figure out how to get it working on my own projects. I had a look at the configure and makefile to try and find out how they did it, but that was no success. I think it's the linking that's causing my problems.
Files of cmockery can be find at:
/usr/local/include/google/cmockery.h
/usr/local/lib/libcmockery.la
/usr/local/lib/libcmockery.a
/usr/local/lib/libcmockery.so.0.0.0
/usr/local/lib/libcmockery.so.0
/usr/local/lib/libcmockery.so
I tried copying the example files calculator.c and calculater_test.c to a separate directory and compile them there.
This is what I did:
gcc -c -o calculator.o calculator.c
gcc -c -o calculator_test.o calculator_test.c -I /usr/local/include/google/
gcc -o run *.o -L /usr/local/lib/
At the last step I got a lot of undefined references to all functions specific to cmockery and the error:
collect2: error: ld returned 1 exit status
I guess I'm messing things up with the linker but I can't find anywhere how it should be done correctly.
You are missing -lcmockery:
gcc -o run *.o -L /usr/local/lib/ -lcmockery

Resources