Question

I want to use the Discount C-library to convert Markdown text into HTML. I have already successfully compiled and installed the library (version 2.1.3).

I tried to compile this code

#include <mkdio.h>
int main(void)
{
 FILE *in, *out;
 MMIOT *doc;

 in = fopen("sample.md", "r");
 out = fopen("out.html", "w");

 doc = mdk_in(in, 0);
 markdown(doc, out, 0);

 ...
}

Explanation: mkd_in()reads the input file ininto the library working-type MMIOT doc and markdown() should convert doc to HTML and writes is to the out file.

with the command gcc -Wall -lmarkdown -o FILE FILE.c and I always get the following output:

undefined reference to `mkd_in(_IO_FILE*, unsigned int)'
undefined reference to `markdown(void*, _IO_FILE*, unsigned int)'

Note: I've run the configuration tool of Discount with the --shared option to build a dynamic library. Default is a static library but with that I've got the same problem.

Was it helpful?

Solution

Try this instead:

gcc -Wall -o FILE FILE.c -lmarkdown

The placement of -l is significant in that many linkers will only use libraries to satisfy unresolved references if they exist at the time when the -l is parsed. This can cause all sorts of problems with circular dependencies, for example.

Where you have it originally, those functions aren't unresolved since you haven't yet compiled FILE.c. When you do compile FILE.c, there's no -l following that point to satisfy the references.

From the gcc man-page:

Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.

And, later, under -l:

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top