2
votes

I am relatively new to C++ and I am using CLions. I am trying to run this code as follows:

/*
 * File: Warmup.cpp
 * ----------------


#include <iostream>
#include <string>
#include "../lib/StanfordCPPLib/console.h"
#include "../lib/StanfordCPPLib/simpio.h"
using namespace std;

/* Constants */

const int HASH_SEED = 5381;               /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33;           /* Multiplier for each cycle      */
const int HASH_MASK = unsigned(-1) >> 1;  /* All 1 bits except the sign     */

/* Function prototypes */

int hashCode(string key);

/* Main program to test the hash function */

int main() {
   string name = getLine("Please enter your name: John");
   int code = hashCode(name);
   cout << "The hash code for your name is " << code << "." << endl;
   return 0;
}

/*
 * Function: hash
 * Usage: int code = hashCode(key);
 * --------------------------------


int hashCode(string str) {
   unsigned hash = HASH_SEED;
   int nchars = str.length();
   for (int i = 0; i < nchars; i++) {
      hash = HASH_MULTIPLIER * hash + str[i];
   }
   return (hash & HASH_MASK);
}

However, I am getting the following errors:

[ 50%] Building CXX object CMakeFiles/Warmup.dir/src/Warmup.cpp.o [100%] Linking CXX executable Warmup ld: library not found for -llib/StanfordCPPLib clang: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: * [Warmup] Error 1 make[2]: [CMakeFiles/Warmup.dir/all] Error 2 make[1]: [CMakeFiles/Warmup.dir/rule] Error 2 make: * [Warmup] Error 2

I know that this works for microsoft studio C++ but I am not sure why it isnt running for CLions. Would someone mind giving some advice here ?

Any help would be greatly appreciated.

Thanks

Edit: This is what my current CMakeLists.txt file looks like:

cmake_minimum_required(VERSION 3.6)
project(0_Warmup)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILE src/Warmup.cpp)
link_libraries(lib/StanfordCPPLib)

add_executable(Warmup src/Warmup.cpp)

Am I making an error linking this library somewhere ?

1
It looks like you are using a library lib/StanfordCPPLiband it cannot be found. is it in the appropriate folder? - Hayt
How does you linker command line actually look? You can add pathes used to lookup libraries with the -L<path> option. - πάντα ῥεῖ
@Hayt In my CMakeLists.txt file, I have link_libraries(lib/StanfordCPPLib) . I also have it in folder stored as lib/StanfordCPPLib where I marked the lib folder as a library root because the compiler does not allow me to import the needed header files when I simply mark StanfordCPPLib as a library root. - PutsandCalls
Do you have a file like StanfordCPPLib.a or StanfordCPPLib.so or StanfordCPPLib.lib - Hayt
@Hayt thanks for the help ! I have been able to work things out and eventually switched over to Visual Studios. Because I was quite a beginner to C++ at the time of writing, I didn't know the many compiler issues that the language faced. Nonetheless, I have figured it out now. Thank you - PutsandCalls

1 Answers

-3
votes

I think u added above this //

#include "../lib/StanfordCPPLib/console.h"
#include "../lib/StanfordCPPLib/simpio.h"// library and u shud try once again a right path of library folder ..