Why linker error when trying to compile SDL? - c

I am watching a seminar on writing 2D games in C, on YouTube here:
https://www.youtube.com/watch?v=yFLa3ln16w0&t=654s
The instructor says to install the SDL software packages, using apt-get install. I already have these files. I checked, and they are in the C search path, in the directory /usr/include/SDL2. The instructor has a GitHub with example code that demonstrates the basics of SDL. Here is the GitHub repository:
https://github.com/tlively/sdl_seminar
Here is SDL's website:
https://www.libsdl.org/
The problem is, when I try to compile the file hello1_sdl.c (in the GitHub repoistory) using gcc -o hello1_sdl hello1_sdl.c, it gives me an error. I looked up what it meant, and it's a linker error. I don't understand why this is happening, as the header files are in the appropriate place.enter image description here
How do I fix this?

The include files for the C/C++ compilers are only half the story. You also need to tell the linker to use the library! If you have everything set up properly, all you should need to do is add -lSDL2 to your final command line (the one with -o that does the linking.)
See more examples at: Lazy Foo' Productions

Related

i386-elf-gcc: error: spawn: No such file or directory

I am following a Github OS development tutorial and I came across a lesson where you need to download i386-elf-gcc and use it to compile sample code.
(respectively lesson https://github.com/cfenollosa/os-tutorial/tree/master/12-kernel-c)
I keep getting the error:
i386-elf-gcc: error: spawn: No such file or directory
when I try to run the command that is IN the lesson:
i386-elf-gcc -ffreestanding -c function.c -o function.o
I have downloaded the tutorial files and there is no possibility that I copied the code incorrectly.
I have been struggling with this error for the past few months and I am really looking forward to an answer.
I am following a Github OS development tutorial and I came across a lesson where you need to download i386-elf-gcc and use it to compile sample code
I have no idea what in that lesson makes you think you're intended to download i386-elf-gcc. Rather, that lesson appears to follow on from the previous one, where you build (and install) a whole cross-compiling toolchain. The expectation is that you will use that toolchain in subsequent lessons that require you to compile C code. Make sure all components of that toolchain are in fact installed, per the instructions in lesson 11. And if the compiler in fact does not work, then the issue is almost certainly with how you built and / or installed it (i.e. lesson 11) not with the command you're trying to use to run it in lesson 12.

Linker cannot find existing Static Library File

I have a Eclipse project using C programming language. I have been stuck with a problem related to linker error since two days now. I have checked various forums to find a solution. Tried a lot of the suggestions but could not resolve it. So as a last resort, i am asking question here. My main program MotorRun.c has code which calls functions in the static library FtMscLib_Static_LIBCMT_Release.Lib which is in Libs folder in the path C:\FT-Project\Common\Libs. I am using MinGW gcc compiler.
When i run the makefile, it generates an error:
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.1/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lC:\FT-Project\Common\Libs\FtMscLib_Static_LIBCMT_Release.Lib
collect2.exe: error: ld returned 1 exit status
The code run by the makefile is
gcc "-LC:\\FT-Project\\Common\\Libs" -shared -o libRoboCopMinGW.exe "src\\MotorRun.o" "-lC:\\FT-Project\\Common\\Libs\\FtMscLib_Static_LIBCMT_Release.Lib"
By looking at the execution code, we can see that the paths and library name has been set correctly, but the linker just cannot find it so that it can link the library with my MotorRun.o object file. Hope someone can help me in finding a solution.
The program MotorRun.c is a very simple one, so i am not posting it here. But if necessary i can update it later. Thanks in advance!
The correct linker syntax is typically something like:
-Lpath_to_library_directory -lname
where the library filename (for a Windows static library) would be name.lib. So your above linker line needs to lose the .lib part. You may also need to prefix the -l argument with another argument -static, to instruct the linker to search for the static library FtMscLib_Static_LIBCMT_Release.Lib otherwise it might try to find the DLL instead.
By the way, there are heaps of posts on StackOverflow regarding the issue of static and dynamic linking with MinGW, so feel free to search for these also. The MinGW web pages also have numerous tips on the same topic.

undefined reference to 'gnutls_...' functions

So for an assignment I need to use GnuTLS. I have downloaded gnutls by following this guide http://www.bauer-power.net/2014/06/how-to-install-gnutls-3123-from-source.html
However, using any of the gnutls functions, similar to how they are used in the official gnutls documentation (http://www.gnutls.org/manual/html_node/Client-examples.html), displays several errors after building. All errors follow this format:
...pathtofile:line#/..undefined reference to 'gnutls_...'
I understand that this might be a linking problem, however I am quite new to this type of environment. What should I do? I have tried to install gnutls again several times.
I am using Ubuntu 14.04, gnutls-3.1.23 and eclipse Luna.
Thanks in advance.
You probably have an error on your link line.
Add the flag -lgnutls to the ld command (or, if just one C file, the compile line).

About linking files using Mac terminal

Okay here goes, I'm completely new at this, started learning the terminal just about 2 days ago. I'm slowly but surely getting the hang of it, now I'm stuck on this and I've been trying to fix it for a good hour. It's a rather simple question as I am a newby.
I have a C file in my desktop and a Header file in a folder in my desktop. I'm including that header in my C file. I have to link them (currently doing a tutorial, it tells me to link, but doesn't show me how).
You have a couple of options. First, you will need to install the software development environment - it's called Xcode. I think you can get it for free on the AppStore, if not Google it.
Then you need to decide if you want to develop and compile graphically in the Xcode Integrated Development Environment. If you do, start Xcode and create a new project and open your C file and change the "include path" to match the location of your header file. Then click "Build" and "Run"
If you want to do things at the commandline, you'll need to install "Xcode Command Line Tools" - Google it. That will give you a compiler. Then you can compile. I'm not certain which compiler you will get - it could be "llvm" or "gcc" or something else, but the command you are looking for will be something like:
gcc -o prog -I /path/to/HeaderFileFolder yoursourcecode.c
which will give you a program called "prog" that you can run by typing
./prog
You are likely confusing two different concepts. The "link" mentioned in the tutorial is probably talking about turning the compiled objects into a single executable. See http://www.cprogramming.com/compilingandlinking.html for an explanation of what linking means in this context.
What you've provided examples of doing is file system linking, which is totally unrelated.
Providing more details on the tutorial could help refine this answer.

How do I use "unity" to unit test C code on Mac (Lion)?

Let me start out by saying that I'm not a C developer and I know very little about actually writing real world C code. I've been doing some research to find a xUnit framework that I can use to write tests for C code and based on what I've found it seems like Unity is the one that I want to go with. It seems simple enough, but I really just don't know what to do after I download the zip file from Unity's website. It doesn't seem to have the normal configure/make/make install, and if it did, I'm not sure that is what I should be using anyway. It does, however, ship with some rake tasks, but none of those seemed to be any kind of "install" task. As a last resort I tried to just copy the 3 source files in with my code (which I really hope is not the right thing to do), but when I try that I get an error trying to compile my c file with gcc, but I think this should be working. Here is my set up:
src/
mycode.c
unity.c
unity.h
unity_internals.h
Here is the source for mycode.c
/* mycode.c */
#include "unity.h"
void test_sample(void)
{
TEST_ASSERT_EQUAL_INT(0, 0);
}
When I run gcc mycode.c I get:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
"_UnityAssertEqualNumber", referenced from:
_test_sample in ccyHByv6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
(I get a similar error when I try to compile unity.c with gcc). Which I assume means that the code that ships with unity requires a different compiler than what I have which is:
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00)
or that maybe unity is not compatible with a 64 bit processor... (I'm running on Mac OS 10.7.3 with a 2.4 GHz Intel Core 2 Duo processor - another thing that may or may not be relavent is that I've got XCode Version 4.3 (4E109) and also Command Line Tools for XCode) At this point I'm just grasping at straws and I'm in way over my head.
My question is, what is the correct process to go through to take a 3rd party C library, such as Unity, and make it available to my C code? Do I need to install something like in Python or Ruby or add something to my path like in Java or something else? Shouldn't just dropping unity's code in with mine work? Am I doing something wrong or is Unity or both? I really just want to be able to test drive C code using Unity. Any help would be greatly appreciated. Thanks in advance!
First, try 'gcc *.c -o mytest'. This will compile all of the C source files into object files, and then link them together into the binary 'mytest'. Keep in mind that all C source files have to be compiled to object files before they can be linked together. (A library is just a bunch of packaged object files.)
If you had a unity library installed in /usr/lib, you could do something like 'gcc mycode.c -lunity -o mytest'. If you had a unity library sitting in the current directory, you might do 'gcc mycode.c ./unity.a -o mytest'. This tells the compiler to look for a file named 'unity.a' in the current directory. Some libraries build .so files ('shared object' files, similar to DLLs in Windows). Replacing 'unity.a' with 'unity.so' should work if that is the case. (I'm assuming a Unix/Linux environment here.)
As an alternative to Unity, look at Google Test, which can be used with C code. I know it is supported on the Mac as well. The primary benefit is a large and active community. More information on Google Test from another SO question: Is Google Test OK for testing C code?
I figured out my problem. It turns out that unity requires you to define a setup and a teardown function and if you do not, you will get errors similar to the one that I was running into.

Resources