Zlib test program won't compile due to refference error - c

I have been reading error LNK2019 for ZLib sample code compiling - But was unable to solve the problem regarding the linking errors.
I downloaded the latest version from zlib.org and assured to inlcude the lib files to my lib folder and include files to my include folder.
However i get something like this:
Tmain.obj : error LNK2019: unresolved external symbol _inflate referenced in function _inf
z_stream strm;
[...]
ret = inflateInit(&strm);
This is from the zpipe.c file, which is one of their test files, so it should compile, but every time it want to call the inflate and deflate functions, it get this linker error, (like on the other question).
I have been trying to resolve it with some of my group members, and we haven't been able to resolve it yet. We are 100% sure that the libraries are correctly placed.

We are 100% sure that the libraries are correctly placed
Getting that wrong would produce a different error. Just putting the .lib file in a directory isn't enough, you also have to tell the linker to link the .lib file. Project + Properties, Linker, Input, Additional Dependencies.
If that doesn't help then document your question better. Post a link to the specific download you used and copy your test project to a file sharing service.

Related

Compile all necessary files with gcc

I’m quite new to C and the gcc compiler. I have a project where I include some other .h files. In that files there are include from more own .h files. And so on. Its a decent amount of includes.
My problem is, when I try to compile the main file with:
gcc -o test tests_main.c
I get the following error:
Undefined symbols for architecture x86_64:
"_waves_secure_hash_test", referenced from:
_crypto_tests in tests_main-d1ae56.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Thats because the compiler cant find the method "waves_secure_hash_test", but its in the included file:
#include "crypto_tests.h"
So, I searched a little bit and found a solution, just compile it with:
gcc -o test tests_main.c crypto_tests.c
Okay, that work for me with that two test file, where the functions are only some test prints.
For my complete project with a decent amount of includes, I can’t write down all that c files to compile... Is there a way to tell the compiler that he has to compile all needed and included files?
You're mixing up included files (.h files, header files) and source files (.c files). You don't have to do anything about header files, the compiler will process them automatically(1).
You do have to list all source files which you want to compile.They are what defines your project. Based on your source tree organisation, you might get away with using shell pattern expansion (e.g. *.c) to get them, though.
For larger projects, you normally don't type the compilation command by hand, but use some project management tool, such as an IDE, a Makefile, or a buildsystem generator such as CMake.
Also note that the error you're actually getting is not a compiler error, it's a linker error. Linking is a separate step that comes after compiling. The compilation reads source files and produces object files. Linking then reads object files and produces a binary (executable or shared library) out of them.
You should also note that there is not always a 1:1 correspondence between header and source files. The classic example are libraries: you can include one or more header files which are shipped with an external library; these will provide declarations for functions implemented inside that library. This allows you to call the functions in your code. To then make these functions available to the linker (so that they can become part of your program), instruct the linker to link the library into your binary (normally via the -l command-line option).
(1) You may still need to point your compiler at directories where these header files reside, which is usually done using the -I command-line option.

Compiling SDL 2 alongside my own source files

Due to a compilation bug I cannot correctly use the SDL dynamically linked, I don't prefer to go with the static linking route as I'm not sure of how well implemented this is in the language I am using.
That's why I would like to compile the SDL 2 source files manually, I simply used the files present under /include and /src in the official repo, but at compile time I get this error:
In file included from /Users/adtrevor/CompileSDL/Sources/SDL/src/video/qnx/video.c:23:
/Users/adtrevor/CompileSDL/Sources/SDL/src/video/qnx/sdl_qnx.h:26:10: fatal error: 'screen/screen.h' file not found
#include <screen/screen.h>
^~~~~~~~~~~~~~~~~
1 error generated.
In file included from /Users/adtrevor/CompileSDL/Sources/SDL/src/video/qnx/keyboard.c:26:
/Users/adtrevor/CompileSDL/Sources/SDL/src/video/qnx/sdl_qnx.h:26:10: fatal error: 'screen/screen.h' file not found
#include <screen/screen.h>
^~~~~~~~~~~~~~~~~
1 error generated.
In file included from /Users/adtrevor/CompileSDL/Sources/SDL/src/video/qnx/gl.c:23:
/Users/adtrevor/CompileSDL/Sources/SDL/src/video/qnx/sdl_qnx.h:26:10: fatal error: 'screen/screen.h' file not found
#include <screen/screen.h>
^~~~~~~~~~~~~~~~~
1 error generated.
I know it is possible to do that since the official website mentions it, but I don't know at all what steps I should follow in order to make it work correctly?
EDIT :
Here is the sample repo: https://github.com/adtrevor/compileSDL
SDL is a cross-platform library, and its unlikely that you want to compile all of the source files in order to get the functionality that you need. Usually you run configure / make, and it will make the decisions on what to build/not build.
You mention that you are building the source files yourself; so you may want to obverse the SDL build and try to capture the same files. In regards to your error, QNX is the Blackberry operating system-- so depending on your use-cases you can avoid building everything under SDL/src/video/qnx/* since you don't have those operating system headers anyway.
SDL should build the static libraries by default anyhow. Consider doing a regular build and using sdl-config --static-libs to find out which static libraries you should link with.

CCS unresolved symbols in linker or compiler causing errors

I am newbee c programmer so be patient. I have a MSP430 C project in Code Composer Studio 7.3 using windows 7. My project (USB) was working fine and then I tried to add more code to incorporate a SPI interface and everything went sideways. I restored my original code but now have problems that weren't there before. Seems to compile but has several unresolved symbols errors in the linker (I think). I have searched/researched many answers to these types of problems on the forum and tried many things (over the past two days!!) and cannot resolve my problem. Possibly I made things worse by copying library/functions directly into my project directory but still didn't seem to change anything. The errors are related to standard MSP430 functions, not my code. My project tree looks like:
project tree
Errors from Console are:
console errors
From everything I have read it indicates that either I have a compiler library path problem or a linker file path problem. Tried a lot of combinations to no avail. Following are the compiler and linker path info:
linker 1
linker 2
If I look in my repository of library stuff downloaded from TI you can see that the dma.h and dma.c files which reference the error message lines in the files are there and I don't understand why the linker can't do its thing.
dma file tree
In the linker under library files I noticed the original libmath.a file didn't point to anything so I found one and added it (not help). Also the libc.a points to a section of 3 directories (different versions of a tool) which each have a different version of libc.a and I think I tried all of them. See below for directories
directories
Any help would be greatly appreciated
A CLUE POSSIBLY: CLUE? In my quest to solve my issues, I created a new CCs project and pasted my main.c file in and then went through a series of compiles solving the errors as the showed up. I was down to one error : can't open source file hal.h. I believe this is a file that was included with the usb example code I started with. After copying it into my project I am back to square on with all the errors listed above. To get here I only needed to point the compiler to driverlib to solve previous errors. The hal.h file seems to only include driverlib.h which is also referenced in my main function so why all the errors??
FIXED BUT WHY!!!
To solve this issue I imported the drivlib directory from the TI repo on my D drive into my project. Now everything compiles. Can anyone tell me why I had to do that given that I had path directives to look on my D drive?? Can it be related to search path length being too long?

Openssl unresolved external BN_is_zero

During the development of a small project using statically linked OpenSSL 1.0.2e I encountered a strange error in VS13:
Error LNK2001: unresolved external symbol _BN_is_zero
Quite often, this happens when you forget to properly liked .lib file in the project properties, but all of the properties were double-checked - they are correct.
Configuration - Release, MT, libs are MT, bh.h included.
And even if one of the libraries is not linked properly, I should have multiple compiler errors complaining about unresolved external symbols of all the elements used in the project, but in my case unresolved external symbol is the only BN_is_zero.
After quick googling there wasn't found any issues, related to openssl bugs and I guess that the problem resides at my project.
Any tips will be appreciated.
UPD
Here is some screens:
I actually forked the csrp github project and compiled it against the particular openssl libraries you are pointing at. I had to slightly modify test_srp.c and src.c to add some code that isn't available on VS2013. A fork of csrp with the changes applied and the visual studio project for VS2013 to compile it can be found here:
https://github.com/sigmoidal/csrp
Note that you need to change the paths of the openssl path on BOTH the debug and release versions to reflect your VS Configuration Properties:
C/C++ > General > Additional Include Directories
D:\dev\openssl\openssl-1.0.2e-vs2013\include
(you should not point it to $openssl_path/include/openssl, it will not work)
and also:
Linker > Input > Additional Dependencies:
For release configuration:
D:\dev\openssl\openssl-1.0.2e-vs2013\lib\libeay32MT.lib
D:\dev\openssl\openssl-1.0.2e-vs2013\lib\ssleay32MT.lib
For debug configuration:
D:\dev\openssl\openssl-1.0.2e-vs2013\lib\libeay32MTd.lib
D:\dev\openssl\openssl-1.0.2e-vs2013\lib\ssleay32MTd.lib
(notice the "d" suffix on debug libs)
Compiled ok for me.
I'm guessing here, but according to the OpenSSL source BN_is_zero is defined as a macro:
http://osxr.org/openssl/source/crypto/bn/bn.h#0407
So perhaps the module that is compiled into srp.obj is making a call to BN_is_zero but is not including bn/bn.h and in the absence of a prototype the compiler is generating a default one.
If this is the case, including the bn.h definitions should avoid the linker error.
It was tricky, but I've finally solved it based on the Drew MacInnis tips.
The matter is that openssl 1.0.2e broke the BN_is_zero functionality by simply removing the macros-containing header from sources. So, the solution is:
Download the bn.h include file from 1.0.1h (or here)
Place bn.h to %OPENSSL_HOME%/crypto/bn directory
Change #include <openssl/bn.h> for #include <../crypto/bn/bn.h>

Linking with DLL in C/C#

I've been trying to link to a third-party DLL. I've tried in 3 different ways: by compiling C in Cygwin, by compiling C++ in Visual Studio and by compiling C# in Visual Studio. Everytime I compile the programs, I get the following errors:
Cygwin: undefined reference to '__imp__IEC61850_Create'
Visual S: LNK2019: unresolved external symbol '__imp__IEC61850_Create'
As you might have gathered, I am trying to call the function 'IEC61850_Create' which is found in the DLL, but it always shows up in errors with the '__imp__' prefix. As the DLL is third-party, I can't view the source. In other places I have searched, people usually talk about an accompanying .o or .lib file. In this case, the only resource I have is the .dll.
Has anyone else experienced something like this, or know how I can link to the library? I can provide sample code if needed.
Thanks.
You really need to link to the .lib file, because only it has the __imp__ stubs needed for static linking.
However, what you can do is to create a .def file and use lib to convert it into a .lib file.
Alternatively you can create a dummy project with empty functions to create a .dll and a .lib file, link to the .lib file, but then use the real .dll with the actual program.
For more information you could read Microsoft KB Article 131313.

Resources