1

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
Jens L.
  • 118
  • 6

1 Answers1

2

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I have the same exact issue as the original poster, but your answer didn't help - any idea why? I'm going crazy! – Vittorio Romeo Aug 06 '13 at 23:59
  • @Vittorio, if it's the _exact_ same problem, the answer will work. Your best bet is to ask another question with the full details, specifically those that are different, otherwise it'll be closed as a duplicate. By asking a question you get the full resources of SO looking into your problem rather than just me :-) – paxdiablo Aug 07 '13 at 00:09
  • I fixed it! Added `extern "C"` in the header include. – Vittorio Romeo Aug 07 '13 at 00:13