Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I compile and run on the "Clion+Cygwin+Win7" platform? #4

Open
Aaron009 opened this issue May 24, 2019 · 3 comments
Open

Can I compile and run on the "Clion+Cygwin+Win7" platform? #4

Aaron009 opened this issue May 24, 2019 · 3 comments

Comments

@Aaron009
Copy link

Aaron009 commented May 24, 2019

I read the documentation carefully and found that I needed libcurl and Google Test, then I successfully installed libcurl according to the documentation.

However, when installing GoogTest , need to install libgtest-dev via apt-get.
Google prompts me to use apt-cyg instead of apt-get and then run as follows:

1.png

When I get here, I can't go on. It seems that TestHttp can't be compiled successfully.

Later, I created a new project myself to test Http, but it still failed. The screenshot is as follows.
2.jpg

CMakeFiles/HTTP.dir/main.cpp.o: In function `main':
/cygdrive/c/Users/Client/Desktop/httpclient-cpp-master/HTTP/main.cpp:11: undefined reference to `CHTTPClient::InitSession(bool const&, CHTTPClient::SettingsFlag const&)'
/cygdrive/c/Users/Client/Desktop/httpclient-cpp-master/HTTP/main.cpp:11:(.text+0x44): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `CHTTPClient::InitSession(bool const&, CHTTPClient::SettingsFlag const&)'
/cygdrive/c/Users/Client/Desktop/httpclient-cpp-master/HTTP/main.cpp:14: undefined reference to `CHTTPClient::DownloadFile(std::string const&, std::string const&, long&)'
/cygdrive/c/Users/Client/Desktop/httpclient-cpp-master/HTTP/main.cpp:14:(.text+0xbc): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `CHTTPClient::DownloadFile(std::string const&, std::string const&, long&)'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/HTTP.dir/build.make:84: HTTP.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:110: CMakeFiles/HTTP.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:122: CMakeFiles/HTTP.dir/rule] Error 2

As for why I want Clion + Cygwin instead of Visual Studio, because I want cpp to compile the application, it can run on Ubuntu or Windows.

@embeddedmz
Copy link
Owner

embeddedmz commented May 24, 2019

Google Test is only needed to run the tests.

I think that your CMake script is missing something like "target_link_libraries(YOUR_BINARY httpclient ${CURL_LIBRARIES}) because your binary (program) is not linked (statically) with the library.

However, if you don't want to link the binary to a statically made library (I think that's your case, you forgot to mention HTTPClient.cpp in : add_executable(YOUR_APP main.cpp HTTPClient.cpp)

Also don't forget to link libcurl to your app (e.g. : target_link_libraries(YOUR_APP curl) )

Hope that helps.

PS : in your code, you have a bug : you need to feed std::unique_ptr a valid pointer so you can do something like this :

#define PRINT_LOG [](const std::string& strLogMsg) { std::cout << strLogMsg << std::endl;  }
std::unqiue_ptr<CHTTPClient>  pHttpCli(new CHTTPClient(PRINT_LOG));

or even better, don't allocate the object in the heap, but instead in the main's stack 👍 (or a global variable)

#define PRINT_LOG [](const std::string& strLogMsg) { std::cout << strLogMsg << std::endl;  }
CHTTPClient  httpClient(PRINT_LOG); // the constructor needs a function to print log messages !

If you need a CMakeLists.txt to run your demo, let me know. If you are not familiar with CMake, you can use a simple makefile, as you are working with an IDE, you have to choose one of these two options. You can also compile directly (g++) and use a text editor...

@Aaron009
Copy link
Author

Aaron009 commented May 27, 2019

According to your prompt, I succeeded. thank you very much.
After adding the following content, still prompted that no curl was found.

target_link_libraries(TestClion curl)

Then I opened the "cygiwn64 setup" and installed the following to succeed. If the picture shows.

1.png

When I was learning CPP, I was based on Visual Studio (because the university teacher taught this, the students around me were based on VS development), so I am not familiar with (CMake, MakeFile, Clang, gcc), I want to learn these things, Are there any recommended tutorials and books?

@embeddedmz
Copy link
Owner

embeddedmz commented May 27, 2019

curl is a program (like wget, you can use it in a terminal to download files) and libcurl-devel are the development files for libcurl (headers and compiled libraries for the OS you are running). You can also compile libcurl by yourself, I showed how to do this for Windows by using a batch script, in Linux you can also do the same thing, the issue is that you have to indicate the path of libcurl headers and its libraries to CMake.

Same thing here, in university, they taught me Visual C++. I learnt "cross-platform" C++ by experience. The concept is not really hard : you have different C and/or C++ files, libraries (C and/or C++) and you have to make a "cake" with it (the cake can be a binary program that you will run, a static or shared library that you may link with another binary later). The obvious solution is to launch several commands involving the compiler (g++ or clang, e.g. g++ module1.cpp -o mo1, g++ module2.cpp -o mo2, g++ mo1 mo2 -o cake -lcurl -lpthread) you can also put all these commands in a shell file (windows or linux).

For simple programs that is fine, but for more bigger ones, you will have many of these g++ instructions and it will be nightmare to manage them, so the solution is to use a makefile script (you can use them in Windows via Visual Studio, Linux, Unix, macOs etc...). when you run "make", the "Makefile" file will be executed by this tool to build you your cake. A Makefile has "rules", you can have a rule to build and run a unit test program, e.g. "make tests" another one to install the library somewhere (in /usr/lib for example - make install).

Personally, I don't like writing makefiles (and it's not easy to write makefiles of portable programs), so under Windows/Visual Studio, the fastest solution is to create a visual studio solution that contains one or more projects (1 solutions -> 1 or more projects, 1 project -> 1 cake). Projects looks like Makefile's rules !

Tools like CMake, will allow you with a single script, to generate Makefiles (Linux/Unix, Visual Studio solutions etc... but the CMake has to be well written to achieve that goal ! Google CMake, you will find a lot of good resources to understand its system (I will send you a list later), you don't need to master it, just understand its concept (at first glance it's not clear).

cmake -G "Visual Studio 17" => will generate Visual Studio 17 solutions (CMakeLists.txt has to be in the current directory, for Windows you may use the CMake-GUI interface).
Under a GNU/Linux, cmake . => will generate a makefile, you can change the "generator" via the -G argument.

What I said applies for other programming languages (For Java, there's Maven etc...). The code is not enough to have the cake, the build system is also very important and it can be complex.

After CMake, you can learn about CPack, that will help you to produce an installer (e.g. NSIS, auto extractible archive etc...) for your project (binary and libraries etc...).

and finally, if you work on a big project with different persons, you may need control version (git - "history" of your source code and its branches) and CI/CD (continuous integration, continuous deployment) tools : gogs, redmine, jenkins, CDash (automate deployment/tests) etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants