Compiling C error - CS50 course - week 1 - Linux - c

i'm trying to compile a simple code by following the steps that the professor made but I have this issue
cc calculator.c -o calculator
/usr/bin/ld: /tmp/ccznJMZB.o: in function `main':
calculator.c:(.text+0x19): undefined reference to `get_int'
/usr/bin/ld: calculator.c:(.text+0x2d): undefined reference to `get_int'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: calculator] Error 1
I'm using Linux mint btw, already had problems installing cs50 library but that's done. I'm happy to get a new error tho, but I need to fix it.
Thanks!

See https://cs50.readthedocs.io/libraries/cs50/c/
To use these functions, make sure to include #include <cs50.h> atop your file and compile with the -lcs50 flag.
#include <cs50.h> requires the header file cs50.h to be in the include path of your compiler.
#include "cs50.h" expects the header file cs50.h to be in the same directory as your .cfile.
The link above also tells you how to install the library:
$ curl -s https://packagecloud.io/install/repositories/cs50/repo/script.deb.sh | sudo bash
$ sudo apt install libcs50
Troubleshooting
If when compiling your program, you see:
/usr/bin/ld: cannot find -lcs50:
Add export LIBRARY_PATH=/usr/local/lib to your .bashrc.
fatal error: 'cs50.h' file not found:
Add export C_INCLUDE_PATH=/usr/local/include to your .bashrc.
cannot open shared object file: No such file or directory:
Add export LD_LIBRARY_PATH=/usr/local/lib to your .bashrc.

Related

How to link the cs50 C library in gcc on windows

I'm new to ะก programming and have been trying to compile my code using MinGW/GCC, but I try to include cs50 (cs50.c, cs50.h) library, and the compiler can't find it. Help me compile who knows what's going on.
I tried to give such command: gcc -LC:\Users\apple\Desktop -lcs50 mario.c
But the result is this:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lcs50
collect2.exe: error: ld returned 1 exit status
Or:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\apple\AppData\Local\Temp\cc8KpeUr.o:mario.c:(.text+0x33): undefined reference to `GetInt'
collect2.exe: error: ld returned 1 exit status
This is my code:
#include <stdio.h>
#include <cs50.h>
int main()
{
int num = GetInt();
printf("%d\n",num);
}
gcc -LC:\Users\apple\Desktop -lcs50 mario.c
There are two problems here.
Always pass libraries after .c files or they won't actually do anything (unless main is in the library).
You appear to have a library called cs50.a; -lcs50 wants to find a file called libcs50.a or libcs50.so.
The easiest way around this problem is to not bother with -L or -l and just pass your library directly to gcc like this:
gcc mario.c cs50.a
Since cs50.c is a single file, you do not need a library at all. You can compile it as needed to save a few steps, it will consume a couple milliseconds more but most of the time you would not notice.
Just use
gcc mario.c cs50.c
and it will work (provided that both files are in the current folder).
I had the same problem. What i did was that i put the cs50.h and cs50.c files in the same folder or directory as stdio.h ; which you will find in the program files of the compiler you're using. It worked for me. Hope this helps.

I have a problem compiling in C using third party libraries with "Scons"

Im creating a program in C and I am going to need some thermodynamics properties for water and steam.
I have searched online and i found this library: http://freesteam.sourceforge.net
On their website they advise to compile using something called "scons".
for that, i must have a file called SConstruct with the following code
# simple SCons script to build the example(s)
env = Environment()
import sys
import platform
if platform.system() == "Windows":
# windows has problems, so we help it along a bit...
cmd = [sys.executable,r'..\freesteam-config']
else:
# on other platforms, it just works...
cmd = ['freesteam-config']
env.ParseConfig(cmd + ['--libs','--cppflags'])
env.Program('test1',['test1.c'])
and run the line $ scons on the command line.
My big problem is that when i run a simple code,
this part of the code doesn't seem to run properly:
#include <freesteam/steam_ps.h>
#include <freesteam/steam_pT.h>
#include <freesteam/region4.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
t = d - (a-d)/(1 + pow((p[0]/c),b));
I just posted the libraries and the line that doesn't seem to work, since the rest is just printfs and scanfs. All those variables are doubles.
and then i get this error
ubuntu#ubuntu:~/Documents/test folder$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o test1 test1.o -L/usr/local/lib -lfreesteam
/usr/bin/ld: test1.o: undefined reference to symbol 'pow##GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
scons: *** [test1] Error 1
scons: building terminated because of errors.
If anyone can help me with this, I would be grateful.
I am not entirely sure, but I think it might be the pow() function.
Although i have tried putting something like pow(2,2) and it works fine.
all libraries that are used need to be listed in the linker command.
this line:
gcc -o test1 test1.o -L/usr/local/lib -lfreesteam
is missing the -lm at the end
Suggest:
gcc -o test1 test1.o -L/usr/local/lib -lfreesteam -lm

Eclipse GCC C Cross Compiling for OpenWrt not working

I am a beginner in both linux and openwrt. Sorry if this is a stupid question.
I am following this tutorial : https://downloads.openwrt.org/docs/eclipse.pdf
Chip : MT7620
Toolchain Prefix : mipsel-openwrt-linux-
Toolchain Path : /ligo/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14
Code:
#include <stdio.h>
void main()
{
printf("Hello World");
}
Error:
**** Build of configuration Debug for project hello ****
make all
Building target: hello
Invoking: Cross GCC Linker
mipsel-openwrt-linux-gcc -L/ligo/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14/lib -o"hello" ./main.o
mipsel-openwrt-linux-gcc: warning: environment variable 'STAGING_DIR' not defined
mipsel-openwrt-linux-gcc: warning: environment variable 'STAGING_DIR' not defined
/ligo/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14/lib/gcc/mipsel-openwrt-linux-musl/5.3.0/../../../../mipsel-openwrt-linux-musl/lib/crt1.o: In function `_start_c':
/ligo/openwrt/build_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14/musl-1.1.14/crt/crt1.c:17: undefined reference to `main'
/ligo/openwrt/build_dir/toolchain-mipsel_24kec+dsp_gcc-5.3.0_musl-1.1.14/musl-1.1.14/crt/crt1.c:17: undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [hello] Error 1
add the following 2 lines at the end of bashrc
export PATH=$PATH:~/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin
export STAGING_DIR=~/openwrt/staging_dir
source ~/.bashrc
To open/create the bashrc you can execute
sudo gedit ~/.bashrc
Well I feel stupid when I have to figure this problem which happens too often.
I get the above error when I forget to save the file that contains main() it gives this error since the file is empty until you save it.
These instructions tell how to automatically save all the modified files when you kick off a build Save before build Keep this set and you won't see this again.
If you don't have the path to the toolchain set in eclipse, it never even finds a compiler, assembler or linker so it can not produce the error given. The warnings about STAGING_DIR are harmless in this case.

Linker issues with dmd and Derelict in Linux: Ld can't find .a files

I am trying to write a test program in D using Derelict2 bindings. Here is my code:
import std.stdio;
import derelict.sfml.audio;
pragma(lib, "/home/ryan/Derelict2/lib/libDerelictSFMLAudio.a");
pragma(lib, "/home/ryan/Derelict2/lib/libDerelictUtil.a");
void main(){
DerelictSFMLAudio.load();
}
All of derelict's .di files are in dmd's default import directory, so I am under the impression that the following command should compile and link my program.
dmd test.d -L-lDerelictSFML -L-lDerelictUtil -L-ldl
However, I am getting this error:
/usr/bin/ld: cannot find -lDerelictSFML
/usr/bin/ld: cannot find -lDerelictUtil
collect2: error: ld returned 1 exit status
--- errorlevel 1
What exactly am I doing wrong?
A little off topic but a concern as well:
What does the -ldl flag do? I know it's needed for a successful link but I'm not sure how and it's not in the ld manual.
You need to pass the library location (e.g. -L-L/usr/lib/dmd) to dmd as well:
dmd test.d -I/usr/include/d -L-L/usr/lib/dmd -L-lDerelictSFML -L-lDerelictUtil -L-ldl
-ldl stands for libdl which is a library that grants your program the ability to link dynamically to libraries.

undefined reference error CUDA, GPUJPEG Library

I have successfully installed this library. My code compiles without issue but I'm getting this error, which I believe is during linking:
suvamsh#vai:~/gpujpeg-lib/src$ nvcc test.cu
/tmp/tmpxft_00002869_00000000-13_test.o: In function 'encoder()':
tmpxft_00002869_00000000-1_test.cudafe1.cpp:(.text+0x23d): undefined reference to `gpujpeg_set_default_parameters(gpujpeg_parameters*)'
collect2: ld returned 1 exit status
I have even set $LD_LIBRARY_PATH to location of the library .so file.
Any help would be greatly appreciated!
As indicated by #talonmies,
You will have to explicitly specify the library to the compiler and supply the path where you installed it, if it is in a "non-standard" place.
Try the following command
$ nvcc test.cu -o test -L"path/to/GPUJPEGLibrary" -l"name_of_GPUJPEGLibrary"
Note that if the name of the .so file is libxyz.so, the "name_of_GPUJPEGLibrary" should be "xyz".
For example, if your .so file is /usr/local/lib/libgpujpeg.so, the cmdline should be
$ nvcc test.cu -o test -L"/usr/local/lib" -l"gpujpeg"

Resources