Undefined reference to jpeg_CreateDecompress() error when using libjpeg - c

I install libjpeg-dev and all files are in the include folder
/usr/include/jerror.h
/usr/include/jmorecfg.h
/usr/include/jpegint.h
/usr/include/jpeglib.h
/usr/include/turbojpeg.h
/usr/include/x86_64-linux-gnu
/usr/include/x86_64-linux-gnu/jconfig.h
And when I try this simple code to decompress a jpeg image I got the error as in title.
here is the code:
#include <stdlib.h>
#include <stdio.h>
#include <jpeglib.h>
int main(void){
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
return 0;
}

The same problem has buged me for about two days!
my solution is use:
gcc your_code.c -ljpeg
instead of:
gcc -ljpeg your_code.c
to compile your code.
here is the explanation:Why does the order in which libraries are linked sometimes cause errors in GCC?
hope this will help.

That sounds like a linking error.
You are probably not linking to the library code; just including the header is not enough, that's not how C works.
Add something like -ljpeg last on your command line.

Related

undefined reference to `__imp_CreateSolidBrush'

Trying to use CreateSolidBrush to change a window background color.
I've included wingdi.h, I believe I've linked gdi32.lib ( however I converted gdi32.lib to a gdi32.a by using LIB2A, and I wonder if this may be an issue? ).
I wouldn't mind using another function but I worry this could be come a re-occuring issue if I'm not able to find a solution.
Some relevant code:
#include <stdio.h>
#include <windows.h>
#include <wingdi.h>
#include <main.h>
DWORD CreateMainWindow(void)
{
.............
WNDCLASSEXA WindowClass = { 0 };
WindowClass.hbrBackground = CreateSolidBrush(RGB(200, 200, 200));
.............
}
I use a function to easily compile
int Compile()
{
................
int result = 0;
char *include = "C:\\Users\\Coding\\C\\src\\include";
char *link = "C:\\Users\\Coding\\C\\src\\lib";
char command[256];
if(snprintf(
command,
sizeof(command),
"gcc -o main -I%s -l gdi32 -L%s main.c", include, link) >= sizeof(command))
{
//exception catching and handling
}
else
{
system(command);
}
return result;
}
I have no reason to believe the file isn't being linked as I'm not receiving an error.
Also I'm only using Notepad++, mingw64, and command prompt.
The error is a linker error, because it can't find the shared library symbol CreateSolidBrush.
All that is needed is linker flag -lgdi32, so it links with MinGW's libgdi32.a.
Don't try to generate this file by converting it from some other file you found which is probably built with a totally different compiler. If you already experimented with that make sure to clean up any lingering gdi32 .a or .lib files from your previous attempts.
Well the answer was extremely simple, linkages and includes must come after the file.
C:\User> gcc main.c -lgdi32 -I<include path> -o main
If this was obvious then I apologize, hopefully this helps another confused individual

Linking files / header files in C

I have 2 c files (& their header files). I have included the function "put" in the corresponding header, but I still have the following errors, when I input "gcc -o main main.c" in the terminal.
main.c:(.text+0x389): undefined reference to `put' collect2: error: ld
returned 1 exit status
may I know the reason? How should I modify my code?
I tried to change the linking order in makefile but failed. Any advice is appreciated, thanks!
CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(Demo)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
add_executable(Demo
main.c main.h KeyValueStore.c KeyValueStore.h )
main.c
#include "main.h"
...
int main() {
...
if (strcmp("PUT", tokens[0]) == 0) {
put(tokens[1], tokens[2]);
...
}
main.h
...
#include "KeyValueStore.h"
...
KeyValueStore.c
#include "KeyValueStore.h"
#define BUFSIZE 1024
typedef struct KeyValueStore {
char key[BUFSIZE];
char value[BUFSIZE];
} KV_Store;
KV_Store kvStore[BUFSIZE];
...
int put(char* key, char* value){
...
}
KeyValueStore.h
...
typedef struct KeyValueStore;
int put(char* key, char* value);
...
Check to see if you have any .o files in that folder and delete them if you do. It's possible the compiler failed at some point while compiling which left *.o files that aren't linked properly
EDIT: I misread the question because for some reason it came up as a c++17 question for me. I'm not sure if what I said still applies to C though I do know it works with C++. Sorry about that to everyone that read my answer before I edited it
Not 100% sure if this is why you are having the error, though you need to put all .c files in the compiler.
So you currently are trying to "gcc -o main main.c" where instead you want to do something more like "gcc -o main main.c keyValueStore.c".
If you do not give the compiler every .c file, it won't have all the definitions and you will get an error similar to what you have.
I also don't really think you need main.h, assuming there isn't any more code in there, it really isn't worth having a whole extra file and instead just putting the #include in main.c.

C undefined reference to InetPtonW

I'm trying to use InetPtonW:
if(InetPtonW(AF_INET, argv[1], &ThisSenderInfo.sin_addr)<=0) {
return 1;
}
However I get the following message when compiling:
warning: implicit declaration of function 'InetPtonW' [-Wimplicit-function-declaration]
undefined reference to `InetPtonW'
collect2.exe: error: ld returned 1 exit status
I've read the documentation located here and I've followed everything but still can't get it to work.
• I'm compiling with Ws2_32 library gcc test.c -o test -lws2_32 using MinGW
• I've included the needed header files #include <ws2tcpip.h> and #include <windows.h>
• I've tried using InetPton but it returns the same error
• Running on Windows 10
I recall running into this exact issue some many months ago. #alk's comment points to a question whose accepted answer feels very similar to what fixed it for me.
You should be able to #define a version macro (or two) before your #include lines to fix it.
While I feel strongly that the aforementioned answer is correct, I'll update this answer later today when I can verify.
Update!
The code I was referencing above doesn't have InetPtonW in it anymore but it had the necessary #defines in it. Here's a brief example that compiles on my machine (win10/mingw64/gcc 8.2.0):
Z:\Some\Directory>gcc test.c -o test -lmswsock -lws2_32
#define NTDDI_VERSION NTDDI_VISTA
#define WINVER _WIN32_WINNT_VISTA
#define _WIN32_WINNT _WIN32_WINNT_VISTA
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
/* This is "test.c", please pardon the lack of error checking. */
int main(void) {
BYTE ipbuf[4] = {0};
WSADATA wsa;
WSAStartup(MAKEWORD(2,2), &wsa);
printf("%d: ", InetPtonW(AF_INET, L"127.0.0.1", &ipbuf));
for(int i = 0; i < 4; ++i)
printf("%hhu.", ipbuf[i]);
WSACleanup();
}
Output should look like:
Z:\Some\Directory>gcc test.c -o test -lmswsock -lws2_32
Z:\Some\Directory>test
1: 127.0.0.1.
Z:\Some\Directory>
It's a linking error. which say that, included library path, the given function not found. please make sure your dll library path for InetPtonW or make sure that is available in your system or not.

c++ struct declaration collect2: ld returned 1 exit status

I managed to solve this issue, but I don't understand why this works like this, so I'm looking for only an clarification for this and any explanations are welcome.
I'm doing the excercises from Bruce Eckel's book: Thinking in C++.
Excercise 13 in Vol 1., Chapter 4 is creating "abstract data type that represents a videotape in a video rental store"
As per the topic of this chapter I assume this should be a struct, like this very simple one:
//video.h
typedef struct {
unsigned id;
char* title;
float length;
int inRent;
void printout();
} Video;
with a dummy implementation, like:
//video.cpp
#include "stdio.h"
#include "video.h"
void Video::printout(){
printf("id:%u, title:%s, length:%f\n",this->id,this->title,this->length);
}
and with a main.cpp like this:
//main.cpp
#include "video.h"
#include "stdio.h"
int main(){
Video first;
first.id=1;
first.title="Some title";
first.length=1.5;
first.printout();
return 0;
}
When trying to compile and link with gcc -o main.exe main.cpp video.cpp -lstdc++, I get this error below at linking. I get the same result with g++ too
C:\Users\jani\AppData\Local\Temp\cc1KIhpp.o:main.cpp:(.text+0x2e): undefined reference to `Video::printout()'
When I use struct identifier - like: typedef struct some_id {...} Video; - compiles and runs without error.
How does this struct declaration work in similar cases?
Eckel says: "You’ll notice that the struct identifier has been left off at the beginning, because the goal is to create the typedef. However, there are times when you might need to refer to the struct during its definition. (...)".
In which cases does this apply?
Many thanks for the answers.
Janos
Why don't you provide the stdio or iostream header in the video.cpp file..These headers provide dynamic linking according to you system. I f you do manual linking just from a simple Makefile, it may not be os & hardware independent.
More importantly, you are not using any C/C++ library functions in the main file, so no need to include stdio.h in the main. Rather include it video.c file
you should compile with g++ (no need to link libstdc++ since all commonly available linux distributions will use libstdc++ by default) and the correct syntax for declaring struct in cpp is:
struct Video
{
std::string title; // note that you used char* for title and assigned it a string literal which is of type const char*, there is no implicit conversion for that.
...
};
Also instead of printf you should use iostream for formatted output, it's more efficient, less error prone and more readable:
#include <iostream>
void Video::printout(){
std::cout << this->id << this->title << this->length << std::endl;
}
It is just a matter of linking order. The following should work:
g++ -o main.exe video.cpp main.cpp -lstdc++
or (for the paranoid):
g++ -o main.exe video.cpp main.cpp video.cpp -lstdc++

C - Undefined reference to function error with linked files

I started implementing a large program. But I ran into a massive issue. So here is very simplified code of my program. I have a separate .c file for my functions which is normal.c the main program is main.c and I have linked those two with cal.h header file.
main.c
#include <stdio.h>
#include "cal.h"
void main()
{
int num1, num2, ans;
num1=5;
num2=5;
ans=add(num1, num2);
printf("%d" ,ans);
}
normal.c
#include "cal.h"
int add(int num1, int num2)
{
return num1+num2;
}
cal.h
#ifndef CAL_H_INCLUDED
#define CAL_H_INCLUDED
#include <errno.h>
int add(int num1, int num2);
#endif // CAL_H_INCLUDED
but when I compile this, it gives out the error
..\main.c|10|undefined reference to `add'|
I'm using CodeBlocks v.13.12 in Windows 8.1 Any answer for this question is much appreciated. I tried with CodeLite as well, but the same error occurs. Thank you!
Complete compilation of the code is required in ubuntu terminal
use the following
gcc normal.c main.c -o out -lm
Code blocks have automatic linking but for that you need to have your source and header files under a project.
I had the same issue when I made individual .c & .h files and expected the IDE to link the object files but it failed. I put them under a project and it worked!
Use complete compilation of your code.
if your c codefiles main.c and normal.c are in ./src and header file cal.h is in ./inc follow below method from current dir(.)
g++ ./src/main.c ./src/normal.c -I ./inc -o main
now main is out binary file to execute.

Resources