Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » undefined reference to `log'(<math.h> was included and compilation works for constants but not variables)
undefined reference to `log' [message #1849606] Wed, 26 January 2022 16:50 Go to next message
Jim De Broux is currently offline Jim De BrouxFriend
Messages: 3
Registered: January 2022
Junior Member
compiles without issue

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define TEST 2.0

int main(int argc, char *argv[]) {
	double test = 0.0;
	double result = 0.0;

	test = 2.0;
	result = log(2);

	printf("\nresult is %f", result);

	return 0;
}

As does this

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define TEST 2.0

int main(int argc, char *argv[]) {
	double test = 0.0;
	double result = 0.0;

	test = 2.0;
	result = log(TEST);

	printf("\nresult is %f", result);

	return 0;
}

BUT this gives an error on compilation

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define TEST 2.0

int main(int argc, char *argv[]) {
	double test = 0.0;
	double result = 0.0;

	test = 2.0;
	result = log(test);

	printf("\nresult is %f", result);

	return 0;
}


The error is shown as :
13:35:39 **** Incremental Build of configuration Debug for project test_fp ****
make all
Building file: ../src/test_fp.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test_fp.d" -MT"src/test_fp.o" -o "src/test_fp.o" "../src/test_fp.c"
Finished building: ../src/test_fp.c

Building target: test_fp
Invoking: GCC C Linker
gcc -o "test_fp" ./src/test_fp.o
./src/test_fp.o: In function `main':
/home/jim/projects/test_fp/Debug/../src/test_fp.c:12: undefined reference to `log'
makefile:43: recipe for target 'test_fp' failed
collect2: error: ld returned 1 exit status
make: *** [test_fp] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

13:35:39 Build Failed. 3 errors, 0 warnings. (took 321ms)

The differences in the code are log with constants and log with variable

Interesting that when I use a constant as an argument the reference to "log" is not a problem but when I try using a variable as an argument it can longer find "log".

So far, Googling around, the info has been scanty, but the best I can determine is that the libraries are being linked in the wrong order. This would seem to me to be an error in the makefile generation and that the gcc compiler has gotten more particular with newer releases. I have tried re-compiling older programs that compiled successfully before and that used <math.h>, but now they no longer compile where once they did.

Here is the make file:
################################################################################
# Automatically-generated file. Do not edit!
################################################################################

-include ../makefile.init

RM := rm -rf

# All of the sources participating in the build are defined here
-include sources.mk
-include src/subdir.mk
-include objects.mk

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif

-include ../makefile.defs

OPTIONAL_TOOL_DEPS := \
$(wildcard ../makefile.defs) \
$(wildcard ../makefile.init) \
$(wildcard ../makefile.targets) \


BUILD_ARTIFACT_NAME := test_fp
BUILD_ARTIFACT_EXTENSION :=
BUILD_ARTIFACT_PREFIX :=
BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),)

# Add inputs and outputs from these tool invocations to the build variables 

# All Target
all: main-build

# Main-build Target
main-build: test_fp

# Tool invocations
test_fp: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS)
	@echo 'Building target: $@'
	@echo 'Invoking: GCC C Linker'
	gcc  -o "test_fp" $(OBJS) $(USER_OBJS) $(LIBS)
	@echo 'Finished building target: $@'
	@echo ' '

# Other Targets
clean:
	-$(RM) test_fp
	-@echo ' '

.PHONY: all clean dependents main-build

-include ../makefile.targets


Since the makefile should NOT be edited I am wondering if I am missing something in the initial set-up of my project or is there something else amiss.

My system info:

:~$ cc --version
cc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I am using Eclipse 2021-12, downloaded directly from eclipse.org

Any information, even just pointing me in the right direction, would be appreciated and likely helpful.

Thanks

[Updated on: Wed, 26 January 2022 18:30]

Report message to a moderator

Re: undefined reference to `log' [message #1849616 is a reply to message #1849606] Wed, 26 January 2022 21:18 Go to previous messageGo to next message
David VavraFriend
Messages: 1430
Registered: October 2012
Senior Member
You need to specify the library and path where log can be found.
With managed build it world be under tool settings for the linker.
Re: undefined reference to `log' [message #1849648 is a reply to message #1849616] Thu, 27 January 2022 18:32 Go to previous messageGo to next message
Jim De Broux is currently offline Jim De BrouxFriend
Messages: 3
Registered: January 2022
Junior Member
David Vavra wrote on Wed, 26 January 2022 16:18
You need to specify the library and path where log can be found.
With managed build it world be under tool settings for the linker.


I believe you misunderstand my issue:

1. the log() function is NOT a user defined function
2. the log() function is part of the standard C library with definitions and info in the math.h header and is part of the environment
3. the <math.h> header file was included in the source file and NOT flagged by the compiler as missing or undefined
4. the log() function worked with constants but NOT variables

Re: undefined reference to `log' [message #1849650 is a reply to message #1849648] Thu, 27 January 2022 18:53 Go to previous messageGo to next message
David VavraFriend
Messages: 1430
Registered: October 2012
Senior Member
You are confusing the header file with the library.
The header defines the interface used by the compiler.
It is NOT the library.

You failed to link the library to your program.
I saw no compilation error.



[Updated on: Thu, 27 January 2022 19:01]

Report message to a moderator

Re: undefined reference to `log' [message #1849661 is a reply to message #1849650] Fri, 28 January 2022 13:20 Go to previous messageGo to next message
Joost Kraaijeveld is currently offline Joost KraaijeveldFriend
Messages: 273
Registered: July 2009
Senior Member
man log says:

LOG(3) Linux Programmer's Manual LOG(3)

NAME
log, logf, logl - natural logarithmic function

SYNOPSIS
#include <math.h>

double log(double x);
float logf(float x);
long double logl(long double x);

Link with -lm.
*****************


Cheers,

Joost
Re: undefined reference to `log' [message #1849666 is a reply to message #1849661] Fri, 28 January 2022 15:59 Go to previous message
Jim De Broux is currently offline Jim De BrouxFriend
Messages: 3
Registered: January 2022
Junior Member
Thank you so much . . . you did point me in the right direction. I found the error of my ways and now the issue is gone.

And I should have known better, I went through the same thing many years ago. I just kept copying and pasting elements from my make files to new make files without thinking about what I was doing.

Now, starting a new project from scratch in a somewhat new IDE I forgot a few things . . . . head-slap

Now it remains to be seen what other things I will forget.

Previous Topic:Searching for Binaries
Next Topic:assembly ide
Goto Forum:
  


Current Time: Thu Dec 01 08:17:41 GMT 2022

Powered by FUDForum. Page generated in 0.01720 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top