Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add an external library in c++ using Cygwin
    primarykey
    data
    text
    <p>I've already wasted time trying to figure this out but no luck so far...I've tried looking through StackOverflow for the same problem but mostly it's related to the IDE people are using, such as VS or Eclipse. </p> <p>I was doing some examples from the Stanford Reader for their C++ course but the code won't work right. I'm trying to figure out how to use external libraries but something keeps going wrong. I'm probably not using the right command but then I don't know which command to use.</p> <p>I'm using Cygwin and it's terminal to do the c++ exercises. I have all the files in the same folder. I am using windows 7 but that shouldn't be the biggest problem though.</p> <p>As an example this error shows well what I get when writing g++ Craps.cpp:</p> <blockquote> <p>$ g++ Craps.cpp</p> <p>/tmp/ccdTdi0t.o:Craps.cpp:(.text+0x1cf): undefined reference to `randomInteger(int, int)'</p> <p>/tmp/ccdTdi0t.o:Craps.cpp:(.text+0x1e6): undefined reference to `randomInteger(int, int)'</p> <p>/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld: /tmp/ccdTdi0t.o: bad </p> <p>reloc address 0x0 in section `.ctors'</p> <p>collect2: ld returned 1 exit status</p> </blockquote> <p>The example I was running this time is just one of the ones with external libraries which gives me the same kind of error if I wrong the other. It won't find the library. </p> <p>This is my main also called Craps.cpp:</p> <pre><code>#include &lt;iostream&gt; #include "random.h" using namespace std; bool tryToMakePoint(int point); int rollTwoDice(); int main() { cout &lt;&lt; "This program plays a game of craps." &lt;&lt; endl; int point = rollTwoDice(); switch (point) { case 7: case 11: cout &lt;&lt; "That's a natural. You win." &lt;&lt; endl; break; case 2: case 3: case 12: cout &lt;&lt; "That's craps. You lose" &lt;&lt; endl; break; default: cout &lt;&lt; "Your point is " &lt;&lt; point &lt;&lt; "." &lt;&lt; endl; if (tryToMakePoint(point)) { cout &lt;&lt; "You made your point. You win." &lt;&lt; endl; } else { cout &lt;&lt; "You rolled a seven. You lose." &lt;&lt; endl; } } return 0; } bool tryToMakePoint(int point) { while (true) { int total = rollTwoDice(); if (total == point) return true; if (total == 7) return false; } } int rollTwoDice() { cout &lt;&lt; "Rolling the dice . . . " &lt;&lt; endl; int d1 = randomInteger(1, 6); int d2 = randomInteger(1, 6); int total = d1 + d2; cout &lt;&lt; "You rolled " &lt;&lt; d1 &lt;&lt; " and " &lt;&lt; d2 &lt;&lt; " - that's " &lt;&lt; total &lt;&lt; endl; return total; } </code></pre> <p>My random.cpp: </p> <pre><code>#include &lt;cstdlib&gt; #include &lt;cmath&gt; #include &lt;ctime&gt; #include "random.h" using namespace std; void initRandomSeed(); int randomInteger(int low, int high) { initRandomSeed(); double d = rand() / (double(RAND_MAX) + 1); double s = d * (double(high) - low + 1); return int(floor(low + s )); } double randomReal(double low, double high) { initRandomSeed(); double d = rand() / (double(RAND_MAX) + 1); double s = d * (high - low); return low + s; } bool randomChance(double p) { initRandomSeed(); return randomReal(0, 1) &lt; p; } void setRandomSeed(int seed) { initRandomSeed(); srand(seed); } void initRandomSeed() { static bool initialized = false; if (!initialized) { srand(int(time(NULL))); initialized = true; } } </code></pre> <p>and lastly my random.h:</p> <pre><code>#ifndef _random_h #define _random_h int randomInteger(int low, int high); double randomReal(double low, double high); bool randomChance(double p); void setRandomSeed(int seed); #endif </code></pre> <p>I hope someone can help me. If it's the Cygwin command that's wrong it would be great if I could see what I should write.</p> <p>Edit: Just found out that I couldn't even write down the examples in the book right. Fixed now and should be without mistakes...I dearly hope so. Sorry about that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload