Elod
Elod

Reputation: 499

C compilation error, external library functions not found

I'm trying to compile a project (which is written on a 32bit OS, Ubuntu). I'm using the given makefiles and I installed all the necessary external libraries (on a 64bit Ubuntu).

I don't get a library linking error but the functions implemented in SDL and argtable2 (external libraries) are not recognized. I only changed the path to the libSDL_gfx.so so that it points to the 64bit installation.

How can i solve this compilation issue?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <argtable2.h>

#include "../../problems/generative_model.h"
#include "../../problems/viewer.h"
#include "opcas.h"


int main(int argc, char* argv[]) {

    double discountFactor;
    unsigned int maxNbEvaluations;
    char isTerminal = 0;
    char isDisplayed = 1;
    char verbose = 0;
    unsigned int nbTimestep = 0;
    double L;

    opcas_instance* instance = NULL;

    state* crtState = NULL;
    state* nextState = NULL;
    double reward = 0.0;
    double* optimalAction = NULL;

    struct arg_dbl* g = arg_dbl1("g", "discountFactor", "<d>", "The discount factor for the problem");
...
    return EXIT_SUCCESS;
}

The makefile looks like:

CC := gcc
FLAGS := -W -Wall -g -ansi -std=c99 -pedantic
LIBS := -lm -lSDL -lSDLmain /usr/lib/x86_64-linux-gnu/libSDL_gfx.so -largtable2
BIN_DIR := bin
OBJ_DIR := obj

all: $(BIN_DIR)/opcas_ball $(BIN_DIR)/opcas_inverted_pendulum 
$(BIN_DIR)/opcas_mountain_car $(BIN_DIR)/opcas_acrobot $(BIN_DIR)/opcas_spring_pendulum

$(BIN_DIR)/opcas_ball: $(OBJ_DIR)/opcas_1.o $(OBJ_DIR)/main_opcas_1.o $(OBJ_DIR)/ball.o $(OBJ_DIR)/viewer_ball.o $(OBJ_DIR)/opcas_drawing_procedure_ball.o $(OBJ_DIR)/svgFile.o
    $(CC) $(FLAGS) $(LIBS) $^ -o $@

The error is (similar error for SDL functions):

gcc -W -Wall -g -ansi -std=c99 -pedantic -lm -lSDL -lSDLmain /usr/lib/x86_64-linux-

gnu/libSDL_gfx.so -largtable2 obj/opcas_1.o obj/main_opcas_1.o obj/ball.o 

obj/viewer_ball.o obj/opcas_drawing_procedure_ball.o obj/svgFile.o -o bin/opcas_ball
obj/main_opcas_1.o: In function `main':
/home/elod/UTwork/RealTime_Cpp/continuous_action_space/algorithms/opcas/main_opcas.c:63: undefined reference to `arg_dbl1'

Upvotes: 0

Views: 1919

Answers (2)

alk
alk

Reputation: 70981

Options specifying libraries need to go into the command-line after the object using symbols out of the libraries given.

So this command:

 gcc -W -Wall -g -ansi -std=c99 -pedantic -lm -lSDL -lSDLmain /usr/lib/x86_64-linux-gnu/libSDL_gfx.so -largtable2 obj/opcas_1.o obj/main_opcas_1.o obj/ball.o obj/viewer_ball.o obj/opcas_drawing_procedure_ball.o obj/svgFile.o -o bin/opcas_ball

should look like this

 gcc -W -Wall -g -ansi -std=c99 -pedantic /usr/lib/x86_64-linux-gnu/libSDL_gfx.so  obj/opcas_1.o obj/main_opcas_1.o obj/ball.o obj/viewer_ball.o obj/opcas_drawing_procedure_ball.o obj/svgFile.o -o bin/opcas_ball -lm -lSDL -lSDLmain -largtable2

As a rule of thumb, which would work in nearly all cases: Place -lxyz options at the end of the link command.

Upvotes: 2

solver115
solver115

Reputation: 101

If you have the source files, compile it in 64 platform and make .so and then link. Else compile with -m32. It will work

Upvotes: -1

Related Questions