[ / / / / / / / / / ] [ dir / ask / dcaco / fur / hypno / o / sl / ttgg / u ][Options][ watchlist ]

/prog/ - Programming

Programming board

Catalog

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Name
Email
Subject
Comment *
File
Select/drop/paste files here
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

[–]

9b00ad (6) No.3298>>3299 [Watch Thread][Show All Posts]

It's probably something stupid and simple, but I can't find it. Everything compiles fine, I can't link the object files.

Link command:


/usr/bin/c++ CMakeFiles/game.dir/game/src/globals.o CMakeFiles/game.dir/game/src/loadOBJ.o CMakeFiles/game.dir/game/src/generator.o CMakeFiles/game.dir/game/src/loadTexture.o CMakeFiles/game.dir/game/src/camera.o CMakeFiles/game.dir/game/src/loadShaders.o CMakeFiles/game.dir/game/src/main.o -o bin/game -rdynamic -lGLU -lGL -lSM -lICE -lX11 -lXext -lGLU -lGLEW -lglfw lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a -lGL -lSM -lICE -lX11 -lXext -lGLEW -lglfw

Output:


CMakeFiles/game.dir/game/src/generator.o: In function `getOpenSimplex(int, int, float**, float, bool)':
/media/pidata/project/MENR/game/src/generator.cpp:16: undefined reference to `open_simplex_noise(long, osn_context**)'
/media/pidata/project/MENR/game/src/generator.cpp:22: undefined reference to `open_simplex_noise4(osn_context*, double, double, double, double)'
/media/pidata/project/MENR/game/src/generator.cpp:26: undefined reference to `open_simplex_noise4(osn_context*, double, double, double, double)'
/media/pidata/project/MENR/game/src/generator.cpp:28: undefined reference to `open_simplex_noise4(osn_context*, double, double, double, double)'
/media/pidata/project/MENR/game/src/generator.cpp:30: undefined reference to `open_simplex_noise4(osn_context*, double, double, double, double)'
/media/pidata/project/MENR/game/src/generator.cpp:40: undefined reference to `open_simplex_noise_free(osn_context*)'
collect2: error: ld returned 1 exit status

If lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a is renamed the command fails with:


c++: error: lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a: No such file or directory

So I know which file it's linking, and that it is finding it. Here is the the output of objdump -t lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a:


In archive lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a:

open-simplex-noise.o: file format elf64-x86-64

SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 open-simplex-noise.c
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .rodata 0000000000000000 .rodata
0000000000000000 l O .rodata 0000000000000010 gradients2D
0000000000000040 l O .rodata 0000000000000048 gradients3D
00000000000000c0 l O .rodata 0000000000000100 gradients4D
0000000000000000 l F .text 00000000000000a0 extrapolate2
00000000000000a0 l F .text 00000000000000ef extrapolate3
000000000000018f l F .text 000000000000012f extrapolate4
00000000000002be l F .text 0000000000000036 fastFloor
00000000000002f4 l F .text 00000000000000bc allocate_perm
0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 l d .eh_frame 0000000000000000 .eh_frame
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000000 *UND* 0000000000000000 free
0000000000000000 *UND* 0000000000000000 malloc
00000000000003b0 g F .text 00000000000000d5 open_simplex_noise_init_perm
0000000000000000 *UND* 0000000000000000 memcpy
0000000000000485 g F .text 00000000000002ef open_simplex_noise
0000000000000000 *UND* 0000000000000000 __stack_chk_fail
0000000000000774 g F .text 0000000000000072 open_simplex_noise_free
00000000000007e6 g F .text 00000000000008fa open_simplex_noise2
00000000000010e0 g F .text 000000000000314b open_simplex_noise3
000000000000422b g F .text 0000000000007c57 open_simplex_noise4


...
0000000000000485 g F .text 00000000000002ef open_simplex_noise
...
0000000000000774 g F .text 0000000000000072 open_simplex_noise_free
...
000000000000422b g F .text 0000000000007c57 open_simplex_noise4
...

9b00ad (6) No.3299>>3300

>>3298 (OP)

I don't know what it means that those symbols are there, and have no idea how linking actually works. But at least the archive isn't empty.

I have checked the declarations of the undefined symbols in the header against the definitions of them in the .c, and the signatures match exactly. I am able to compile and link the test program that the library comes with.


gcc lib/open-simplex-noise-in-c/open-simplex-noise-test.c -rdynamic lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a -lpng

libpng is only required by the test program, not the library itself.

So I suspect it's the link order of the original command:


... -rdynamic -lGLU -lGL -lSM -lICE -lX11 -lXext -lGLU -lGLEW -lglfw lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a -lGL -lSM -lICE -lX11 -lXext -lGLEW -lglfw

I have tried moving it to the start:


... -rdynamic lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a -lGLU -lGL -lSM -lICE -lX11 -lXext -lGLU -lGLEW -lglfw -lGL -lSM -lICE -lX11 -lXext -lGLEW -lglfw

and to the end:


... -rdynamic -lGLU -lGL -lSM -lICE -lX11 -lXext -lGLU -lGLEW -lglfw -lGL -lSM -lICE -lX11 -lXext -lGLEW -lglfw lib/open-simplex-noise-in-c/bin/libopen-simplex-noise.a

and receive the exact same errors as initially. I then tried putting it in literally every other possible position after -rdynamic, none of those succeeded either.

open-simplex-noise.c compiles with just:


gcc -c open-simplex-noise.c -Wall -Wpedantic

And the only warnings are about C99 long long constants:


open-simplex-noise.c: In function open_simplex_noise’:
open-simplex-noise.c:194:16: warning: use of C99 long long integer constant [-Wlong-long]
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
^
open-simplex-noise.c:194:40: warning: use of C99 long long integer constant [-Wlong-long]
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
^
open-simplex-noise.c:195:16: warning: use of C99 long long integer constant [-Wlong-long]
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
^
open-simplex-noise.c:195:40: warning: use of C99 long long integer constant [-Wlong-long]
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
^
open-simplex-noise.c:196:16: warning: use of C99 long long integer constant [-Wlong-long]
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
^
open-simplex-noise.c:196:40: warning: use of C99 long long integer constant [-Wlong-long]
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
^
open-simplex-noise.c:198:17: warning: use of C99 long long integer constant [-Wlong-long]
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
^
open-simplex-noise.c:198:41: warning: use of C99 long long integer constant [-Wlong-long]
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
^

Clearly, since the test program works, the problem is with my code and not the library, it's header, or the .a. What could I have done to cause this?

To recap: .a and .h are just fine, linker is finding the .a, moving the library name around in the link command doesn't help, the declaration and definition signatures match.


9b00ad (6) No.3300

>>3299

Here is the C++ code that calls into libopen-simplex-noise.a:


#include "generator.h"
#include <vector>
#include <random>
#include <iostream>
#include "open-simplex-noise.h"
using namespace glm;
using namespace std;

void getOpenSimplex (int width, int height, GLfloat feature_size, bool singleOctave, GLfloat** data) {
int x, y;
double value;
double v0, v1, v2; // values from different octaves.
struct osn_context *ctx;

open_simplex_noise(77374, &ctx);

for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
//Get the value for (x,y)
if (singleOctave) {
value = open_simplex_noise4(ctx, (double) x / feature_size, (double) y / feature_size, 0.0, 0.0);
} else {
// Use three octaves: frequency N, N/2 and N/4 with relative amplitudes 4:2:1.
v0 = open_simplex_noise4(ctx, (double) x / feature_size / 4,
(double) y / feature_size / 4, 0.0, 0.0);
v1 = open_simplex_noise4(ctx, (double) x / feature_size / 2,
(double) y / feature_size / 2, 0.0, 0.0);
v2 = open_simplex_noise4(ctx, (double) x / feature_size / 1,
(double) y / feature_size / 1, 0.0, 0.0);
value = v0 * 4 / 7.0 + v1 * 2 / 7.0 + v2 * 1 / 7.0;
}

//rgb = 0x010101 * (uint32_t) ((value + 1) * 127.5);
//data[x][y] = (0x0ff << 24) | (rgb);
data[x][y] = value;
}
}

open_simplex_noise_free(ctx);
}
/*
rest of the file commented out, still gives the same errors so likely not relevant.
*/


9b00ad (6) No.3301

Here is the header, "open-simplex-noise.h":


#ifndef OPEN_SIMPLEX_NOISE_H__
#define OPEN_SIMPLEX_NOISE_H__

/*
* OpenSimplex (Simplectic) Noise in C.
* Ported to C from Kurt Spencer's java implementation by Stephen M. Cameron
*
* v1.1 (October 6, 2014)
* - Ported to C
*
* v1.1 (October 5, 2014)
* - Added 2D and 4D implementations.
* - Proper gradient sets for all dimensions, from a
* dimensionally-generalizable scheme with an actual
* rhyme and reason behind it.
* - Removed default permutation array in favor of
* default seed.
* - Changed seed-based constructor to be independent
* of any particular randomization library, so results
* will be the same when ported to other languages.
*/

#if ((__GNUC_STDC_INLINE__) || (__STDC_VERSION__ >= 199901L))
#include <stdint.h>
#define INLINE inline
#elif (defined (_MSC_VER) || defined (__GNUC_GNU_INLINE__))
#include <stdint.h>
#define INLINE __inline
#else
/* ANSI C doesn't have inline or stdint.h. */
#define INLINE
#endif

struct osn_context;

int open_simplex_noise(int64_t seed, struct osn_context **ctx);
void open_simplex_noise_free(struct osn_context *ctx);
int open_simplex_noise_init_perm(struct osn_context *ctx, int16_t p[], int nelements);
double open_simplex_noise2(struct osn_context *ctx, double x, double y);
double open_simplex_noise3(struct osn_context *ctx, double x, double y, double z);
double open_simplex_noise4(struct osn_context *ctx, double x, double y, double z, double w);

#endif


9b00ad (6) No.3302

File (hide): 1443662329406.jpg (101.79 KB, 700x520, 35:26, awoo 1.jpg) (h) (u)

Thanks for reading, have an awoo.


b57317 (2) No.3305

Use extern "C".

C++ uses symbol mangling, so when you compile and archive libopen-symplex-noise.a with your C compiler, the symbols are different than when you include the header with your C++ compiler.

You have 2 easy options.

Either replace


#include "open-simplex-noise.h"

With


extern "C"
{
#include "open-simplex-noise.h"
}

Or do the better and more idiomatic method of putting the following into your C header (and all your future C headers):


#ifndef OPEN_SIMPLEX_NOISE_H__
#define OPEN_SIMPLEX_NOISE_H__

#ifdef __cplusplus
extern "C" {
#endif

// Blah blah blah

#ifdef __cplusplus
}
#endif

You probably should also not be afraid of #pragma once. Every noteworthy compiler supports it.

Please read http://sscce.org/ before asking for help again. It would have been much easier to help if you had worked this down to a simple foo C function first without your preprocessor directives.

This is an example of your problem when trimmed down to its simplest form:

foo.h:


#pragma once

void foo();

foo.c:


#include "foo.h"
#include <stdio.h>
void foo()
{
puts("Foo!");
}

test.cxx:

           
#include "foo.h"

int main()
{
foo();
}

Compilation attempt:


$ gcc -c -ofoo.o foo.c
$ g++ -c -otest.o test.cxx
$ g++ -o test test.o foo.o
test.o: In function `main':
test.cxx:(.text+0x5): undefined reference to `foo()'
collect2: error: ld returned 1 exit status
$

And when fixed:

foo.h:


#pragma once
#ifdef __cplusplus
extern "C" {
#endif

void foo();

#ifdef __cplusplus
}
#endif

Compilation and running:


$ gcc -c -ofoo.o foo.c
$ g++ -c -otest.o test.cxx
$ g++ -o test test.o foo.o
$ ./test
Foo!
$


9b00ad (6) No.3310>>3311

File (hide): 1443706300910.jpg (904.22 KB, 1733x2000, 1733:2000, awoo 3.jpg) (h) (u)

Thanks a lot, sorry for the verbosity of the question. I'll do better if I have questions in the future.


b57317 (2) No.3311

>>3310

No worries; it wasn't inconvenient, just necessary reading when you're trying to get programming help online. It also helps for your own debugging as well.

Happy hacking!




[Return][Go to top][Catalog][Screencap][Update] ( Scroll to new posts) ( Auto) 5
7 replies | 2 images | 2 UIDs | Page ?
[Post a Reply]
[ / / / / / / / / / ] [ dir / ask / dcaco / fur / hypno / o / sl / ttgg / u ][ watchlist ]